Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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 can't seem to figure out how I can implement it.

 

For example - I have an XML (C:\file.xml) such as the following:


<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>
[/Code]

 

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 ...
[/Code]

 

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 Folder's 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 ...?
[/Code]

 

Note - I don't 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,

  • Administrators
Posted

If you load the xml into an XmlDocument then XPath can be used to locate the node e.g.

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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...