The XML file structure is like this.
<?xml version="1.0" encoding="UTF-8"?>
<Application>
<Tabs>
<Tab name="1">
<title>abcd</title>
<description>xyz</description>
</Tab>
<Tab name="2">
<title>abcde</title>
<description>xyzw</description>
</Tab>
</Tabs>
<Files>
</Files>
</Application>
I would like to read only the Tabs section using XmlReader in asp.net 2.0. The values that I'm interested in are the title and description contents. There are in total 7 tabs which can increase also later on. Hence can't iterate over a count variable with value fixed.
If you can use XPathDocument
, you can try something like this.
Note: If you already have a XmlReader
instance, instead of using StringReader
, you can use the constructor overload that take XmlReader
.
string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<Application>
<Tabs>
<Tab name=""1"">
<title>abcd</title>
<description>xyz</description>
</Tab>
<Tab name=""2"">
<title>abcd</title>
<description>xyzw</description>
</Tab>
</Tabs>
<Files>
</Files>
</Application>";
string xpath = "/Application/Tabs/Tab/description";
XPathDocument doc = new XPathDocument(new StringReader(xml));
XPathNavigator nav = doc.CreateNavigator();
XPathNodeIterator nodeIterator = nav.Select(xpath);
foreach (XPathNavigator item in nodeIterator)
{
Console.WriteLine(item.Value);
}