XmlDocument GetElementsByTagName within a specified block in C#



I have an xml file and currently I am getting element-by-tag-name. What I am trying to achieve is to specify which block to use, such as bookstore or shop. Thank you for any help and advice.


XML:



<VariablesSpecs name="Data01">
<bookstore>
<book genre='novel' ISBN='10-861003-324'>
<title>The Handmaid's Tale</title>
<price>19.95</price>
</book>
</bookstore>
<shop>
<book genre='novel' ISBN='10-861003-324'>
<title>The Handmaid's Tale</title>
<price>19.95</price>
</book>
</shop>
</VariablesSpecs>


Code:



var doc = new XmlDocument();
doc.Load("data.xml");

var bookNodes = doc.GetElementsByTagName("book");
foreach (var bookNode in bookNodes)
{
// Collect data.
}

No comments:

Post a Comment