views:

199

answers:

2

I have a XmlDocument which is not properly formed

<library>
  <dept>
    <books></books>
    <language></language>
  </dept>
  <dept>
    <lecturer></lecturer>
  </dept>
</library>

I want to do a XmlDocument.SelectSingleNode for 'lecturer' tag.

When I select ChildNodes of <library> tag, I get only <books> and <language> but not <lecturer>. How do I get XmlNode object of tag?

+1  A: 

The XML is well formed XML. It would not load into a XmlDocument otherwise.

The only ChildNodes of library are dept nodes.

To get lecturer, you can do the following:

XmlDocument.SelectSingleNode("library/dept/lecturer");
Oded
+1 (with that small edit :) )
Jeff Sternal
Thanks @Jeff :)
Oded
Thanks Jeff. I missed one more tag in there. <location>. How do I get to lecturer tag now?<library> <dept> <books></books> <language></language> </dept> <location></location> <dept> <lecturer></lecturer> </dept> </library>Thank youSid
Reddy S R
@user315445 (Sid) - it doesn't change, as the `location` tag is in the same _level_ as `dept`.
Oded
Actually I had to parse a csproj file but I had problems in getting the PostBuildEvent tag. So, instead of using selectSingleNode, I used GetElementsByTagName(). And it solved my problem.Thanks for the answer Jeff else I would have spent more time on SelectSingleNode method.
Reddy S R
A: 

To parse csproj file, use GetElementsByTagName(). I dont know why SelectSingleNode() is not working!

Thank you Sid

Reddy S R
If this is a new question, you should add it as a new question. For info, with csproj the issue is usually that it is namespaced xml, which is... trickier.
Marc Gravell