-1

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?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Null Head
  • 2,877
  • 13
  • 61
  • 83

2 Answers2

2

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");
Jeff Sternal
  • 47,787
  • 8
  • 93
  • 120
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • Thanks Jeff. I missed one more tag in there. . How do I get to lecturer tag now? Thank you Sid – Null Head Apr 14 '10 at 06:17
  • @user315445 (Sid) - it doesn't change, as the `location` tag is in the same _level_ as `dept`. – Oded Apr 14 '10 at 06:41
  • 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. – Null Head Apr 14 '10 at 07:00
0

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

Thank you Sid

Null Head
  • 2,877
  • 13
  • 61
  • 83
  • 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 Apr 14 '10 at 12:05