XML newbie

  • Thread starter Thread starter Kim
  • Start date Start date
K

Kim

Im having problems getting the correct number from
System.Xml.XmlNodeList.Count so my loop will process all nodes named
<Folder>.
What am I doing wrong?

XML file (cut out):
<Configuration>
<Options>
<Settings>
<SourceFolders>
<Folder>C:\</Folder>
<Folder>D:\Test\</Folder>
</SourceFolders>
</Settings>
</Options>
</Configuration>

C# code:
System.Xml.XmlNodeList appItems =
appDoc.SelectNodes("Configuration/Options/Settings/SourceFolders");
System.Xml.XmlNode appItems_child;

for (int i = 0; i < appItems.Count; i++)
{
appItems_child = appItems.Item(i).SelectSingleNode("Folder");
<do other things here>
}
 
Found a solution, but I still get WHY it works. Can someone explain it
to me?

System.Xml.XmlNodeList appItems =
appDoc.SelectNodes("Configuration/Options/Settings/SourceFolders/Folder");
System.Xml.XmlNode appItems_child;

for (int i = 0; i < appItems.Count; i++)
{
appItems_child = appItems.Item(i);
<do other things here>
}


Kim skrev:
 
I'm not sure what is to explain...?
...SelectNodes("Configuration/Options/Settings/SourceFolders");
This says "find all the SourceFolders instances based on the path..."
for (int i = 0; i < appItems.Count; i++) {
appItems_child = appItems.Item(i).SelectSingleNode("Folder");
<do other things here>
}
This says "for each SourceFolders instance we just found, select the first
Folder child, and do something" (then the next SourceFolders instance) -
hence d:\Test\ will never be found

Conversely:
...SelectNodes("Configuration/Options/Settings/SourceFolders/Folder");
This says "find all Folder instances based on the path..."
for (int i = 0; i < appItems.Count; i++) {
appItems_child = appItems.Item(i);
<do other things here>
}
This says "for each Folder instance we just found, do something"

So the SelectSingleNode is the problem in the first case... it will never
spot a second matching node.

Does this help?

Marc
 
Does this help?

Somehow. I think my mistake was to think of the XPath search string as
a folder path (like C:\Windows\System). This was making me think "jump
to <xpath string> location and search for <Folder> in there", and while
that took the first child made it more confusing.

Maybe I just needed to hear it from another.
Thanks Marc G.
 
Back
Top