3

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

Console.WriteLine(xmlDocument.SelectSingleNode("//dataTemplateSpecification/templates/template/elements/element/@name").Value.ToString());

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

这是代码...

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);
            }
        }
    }
}

这是我使用的 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>

我只是向您展示了您需要了解 xml 结构的部分。我向你保证,它的格式很好。我想我以前问过这个问题,但不知何故没有发布(也许我忘记了,谁知道)。对此的任何帮助将不胜感激。如果我想出为什么它不起作用的原因,我一定会让你们知道。

谢谢你。

4

2 回答 2

7

为什么你不能使用这个XPath

xmlDocument.SelectSingleNode("//templates/template/elements/element/@name").Value
于 2011-07-05T13:04:15.277 回答
1

您需要在代码中指定 XML 文件的命名空间。
有关更多信息,请参见此处:如何在根节点具有属性时选择 xml 根节点?

于 2011-07-05T12:55:49.910 回答