I have an xml file that is structured like the following (I've simplified for readability). I can only be certain that the recipe id is unique. I need to pull the settings for just recipe1.group1.
I can get the file into an xml reader, and then into a dataset from there. How do I filter on the data set to just get the setting rows for recipe1.group1? Ideally, I'd like something like the following pseudo-code.
DataTable settings = RecipeDataSet.Tables.Select('Get settings from Group where Recipe='recipe1' and Group='group1')
Thanks,
SteveJ
etc......
Mahesh ChandPosted Apr 20, 2010, 12:21 PM
Steve JacksonPosted Apr 20, 2010, 3:13 PM
myXmlDoc.load("filename.xml");
string _xpath = String.format(@"/doc_element/recipe[@id=" "{0}" "]/group[@id=" "{1}" "]/setting";
XmlNode _rootElement = myXmlDoc.DocumentElement;
XmlNodeList _matchingSettings = root.SelectNodes(_xpath);
foreach (xmlNode _node in _matchingSettings){
............................
}
I didn't test the above, but it should get someone started. Also, see the W3C web page for xpaths - it is really useful.
http://www.w3schools.com/xpath/xpath_syntax.asp
Cheers;
Steve JacksonPosted Apr 20, 2010, 11:15 AM
I did look at select, but couldn't get the details worked out. The difficulty is that I want to sort a table based on what parent it belonged to, which I can't seem to work out. In my example above, I want to sort settings based on the id of recipe and group. Maybe a better way to ask the question would be to know if I can extract sub tables from tables. That is to say, If I pull the recipe table from the dataset, can I pull from it the group table, then the settings table (only including the desired settings?)
Pseudo:
Get recipe table from xml-based dataset
From recipe table, get only rows for recipe id='recipe1', call it recipe1 table
From recipe1 table, get only group1 rows, call it group1 table
From group1 table, get the settings table.
Mahesh ChandPosted Apr 19, 2010, 7:35 PM
Here is an example.