How to extract specific information from XML [C# 2003]

Shaitan00

Well-known member
Joined
Aug 11, 2003
Messages
343
Location
Hell
I have an application that reads data from an XML file, typically I simply use the "DataTable.ReadXML(sXML)" and then extract the information that I need, but not I need to do it based on a condition and I cant seem to figure out how I can implement it.

For example - I have an XML (C:\file.xml) such as the following:
Code:
<RootNode>
    <Stores>
      <Division Name="A">
         <Info>
            <Segment Folder="FolderA1"/>
            <Segment Folder="FolderA2"/>
            <Segment Folder="FolderA3"/>
         </Info>
      </Division>
      <Division Name="B">
         <Info>
            <Segment Folder="FolderB1"/>
            <Segment Folder="FolderB2"/>
            <Segment Folder="FolderB3"/>
         </Info>
      </Division>
   <Stores>
</RootNode>

As you can see, my XML has two divisions (name "A" and "B"), I need to get the Folder information depending on which division I am dealing with. Specifically something like this (pseudo-code):

Code:
if (Division = varX)			// where varX is either "A" or "B"
   for each (string sFolder in <Segment Folder="..."> in <Info>)	// Listing all the Segment Folders from either "A" or "B" depending on varX
      ... do something with sFolder ...

Now, I have the following code that simply dumps the XML into a DataTable, but I have no clue how, once with the datatable, to extract only the Folders from <Segment Folder=".." /> of the appropriate <Info></Info> from the appropriate <Division Name=".."></Division> where the Division Name matches my search criteria...

Code:
DataSet dsxmlfile = new DataSet();
dsxmlfile.ReadXml(sXML);
DataView dsview = dsxmlfile.Tables["Stores"].DefaultView;

...? stuck here ...?

Note - I dont need to use DataTable.ReadXML to perform my work, it is simply the easiest way I know how to input an XML file, however I would assume this may no longer suit my needs.

Any help would be greatly appreciated.
Thanks,
 
If you load the xml into an XmlDocument then XPath can be used to locate the node e.g.
C#:
XmlDocument doc = new XmlDocument();
doc.Load("XMLFile1.xml");
XmlElement ele = (XmlElement) doc.SelectSingleNode("//Stores/Division[@Name=A]");
Another possibility is XMLSerialization - either way they avoid the overheads of datasets / datatables.
 
Back
Top