XmlDocument and getting specific Attributes using xPath

SoftwareSavant picture SoftwareSavant · Jul 5, 2011 · Viewed 9.4k times · Source

I have seen a couple of examples on here where Xpath is used in conjunction with XmlDocument to get a specific attribute from an XmlDocument Node.... Example

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

For some reason I am getting a "Object reference not set to an instance of an object." exception. Whenever I run across that particular line of code. I have a little test app that I have set up to test out different things before I put them into my main project...

Here is the code for that...

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

Here is part of the XML that I used...

    <?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>

I just showed you the part that you need to understand the xml structure. I assure you that it is well formed. I think I asked this question before but somehow or the other it didn't get posted (maybe I forgot, who knows). Any help with this would be greatly appreciated. If I come up with a reason for why it isn't working I will be sure to let you guys know.

Thank You.

Answer

VMAtm picture VMAtm · Jul 5, 2011

Why can't you use this XPath:

xmlDocument.SelectSingleNode("//templates/template/elements/element/@name").Value