Shaitan00 Posted September 25, 2008 Posted September 25, 2008 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, Quote
Administrators PlausiblyDamp Posted September 26, 2008 Administrators Posted September 26, 2008 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.