XmlDocument并使用xPath获取特定属性

Sof*_*ant 3 c# xml xpath xmldocument xml-attribute

我在这里看到了几个例子,其中Xpath与XmlDocument一起使用,以从XmlDocument节点获取特定属性....

Console.WriteLine(xmlDocument.SelectSingleNode("//dataTemplateSpecification/templates/template/elements/element/@name").Value.ToString());
Run Code Online (Sandbox Code Playgroud)

出于某种原因,我得到一个"对象引用未设置为对象的实例".例外.每当我遇到特定的代码行时.我有一个小测试应用程序,我已经设置为测试不同的东西,然后我把它们放入我的主项目...

这是代码......

namespace ReadXml
{
    class Program
    {
        static void Main(string[] args)
        {
            //string fulXmlPath =     System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/templateExample.xml");
            XDocument xDocument = XDocument.Load("C:\\Users\\derekww\\Documents\\XML Documents\\templateExample.xml");
            XElement elem = xDocument.Element("dataTemplateSpecification"); ;
            XmlDocument xmlDocument = new XmlDocument();
            StreamReader file = new StreamReader("C:\\Users\\derekww\\Documents\\XML Documents\\templateExample.xml");

            xmlDocument.Load(file);

            //XmlDocument theDoc = new XmlDocument();
            //using (var xmlReader = xDocument.CreateReader())
            //{
            //    xmlDocument.Load(xmlReader);
            //}

            //Console.WriteLine(elem.ToString());
            XmlNode xNode = xmlDocument.SelectSingleNode("//dataTemplateSpecification/templates/template/elements/element");
            Console.WriteLine("WORK PLEASE!!!! {0}", xNode.Value.ToString());
            //Console.WriteLine(xmlDocument.SelectSingleNode("//dataTemplateSpecification/templates/template/elements/element/@name").Value.ToString());
            //Console.WriteLine("This better Work>>>> {0}", xmlDocument.Attributes["/dataTemplateSpecification/templates/template/elements/element/@name"].Value);
            Console.ReadLine();
            //Console.WriteLine("This better Work>>>> {0}", xmlDocument.SelectSingleNode("//dataTemplateSpecification/templates/template/elements/element/@name").Value);
            //foreach (String AttVal in xmlDocument.SelectSingleNode("//dataTemplateSpecification/templates/template/elements/element/@name").Value)
            {
                //Console.WriteLine("This better Work>>>> {0}", AttVal);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我用过的XML的一部分......

    <?xml version="1.0" encoding="utf-8"?>
    <dataTemplateSpecification id="id1" name="name1" xmlns="http://EADIS.upmc.com      /DataTemplateSpecification.xsd">
      <description xmlns="">
        <html>text</html>
      </description>
      <templates xmlns="">
        <template>
          <elements>
            <element id="element0" name="PatientId" display="Patient ID"  dataType="String" value="0101010111111" visable="false" readOnly="true">
              <validation>
                <rules>
                  <rule id="0" test="#element0.value == ''">
                    <fail>
                      <html><b>Patient ID is null, value must be present</b></html>
                    </fail>
                  </rule>
                </rules>
              </validation>
            </element>
           </elements>
          </template>
         <templates>
Run Code Online (Sandbox Code Playgroud)

我刚刚向您展示了理解xml结构所需的部分.我向你保证,它已经很好了.我想我之前问过这个问题,但不管怎么说还是没有发布(也许我忘了,谁知道).任何有关这方面的帮助将不胜感激.如果我想出一个为什么它不起作用的原因,我一定会让你们知道.

谢谢.

VMA*_*Atm 7

为什么你不能用这个XPath:

xmlDocument.SelectSingleNode("//templates/template/elements/element/@name").Value
Run Code Online (Sandbox Code Playgroud)