0

I am trying to read in a simple xml file but am having some issues. Here is the code to read it:

    XmlDocument doc = new XmlDocument();
    doc.Load("C:/file.xsd");

    XmlNode loc = doc.SelectSingleNode("/schema/annotation/appinfo");
    XmlNodeList refNode= loc.SelectNodes("referral");

And here is what the XML looks like:

<schema>
  <annotation>
   <appinfo>
     <referral/>
     <referral/>
     <referral/>
   </appinfo>
  </annotation>
</schema>

I've simplified it for readabilities sake, but I am trying to read in the 'referral' nodes. I confirmed that the file is being read in however the 'loc' variable is null.

Would appreciate some help with this, thanks!

john cs
  • 2,220
  • 5
  • 32
  • 50
  • 1
    is there a namespace in the un-simplified version? like in this question http://stackoverflow.com/questions/24734/selectnodes-not-working-on-stackoverflow-feed – Jonesopolis May 01 '13 at 20:37
  • no namespaces, the name of the nodes are exactly as listed (just removed attributes here) – john cs May 01 '13 at 20:39
  • give doc.SelectSingleNode("schema/annotation/appinfo"); a try. (remove leading slash) – Jonesopolis May 01 '13 at 20:40
  • 1
    What's the full error? where in your code is it happening? – tnw May 01 '13 at 20:42
  • To add on to Jonesy I notice that you reference an xsd which most always has a namespace. Adding a namespace manager as shown in the question he referenced should suffice Example: – Bryan Roberts May 01 '13 at 20:45

1 Answers1

0

Following code giving correct results, so there is no problem with selecting nodes by given path. i think you have to add correct namespace to work.

string xml = @"<schema>
                <annotation>
                <appinfo>
                    <referral/>
                    <referral/>
                    <referral/>
                </appinfo>
                </annotation>
            </schema>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

XmlNode loc = doc.SelectSingleNode("/schema/annotation/appinfo");
XmlNodeList refNode = loc.SelectNodes("referral");
Damith
  • 62,401
  • 13
  • 102
  • 153