I have this xml file:
<Apoens>
<valuta category ="HRK">
<value>25</value>
<name>25</name>
</valuta>
<valuta category ="HRK">
<value>5</value>
<name>5</name>
</valuta>
<valuta category ="EUR">
<value>25</value>
<name>25</name>
</valuta>
<valuta category ="EUR">
<value>5</value>
<name>5</name>
</valuta>
</Apoens>
I would like to get data for category "EUR" ?
Xml attribute values
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
David Azzopardi10 pointsPosted Jul 16, 2009, 1:06 AM EDT
Try something like this:
string fileName = @"C:\data.xml";
XPathDocument doc = new XPathDocument(fileName);
XPathNavigator nav = doc.CreateNavigator();
// xpath: //valuta[@category='EUR']/value
// This returns each value node where category="EUR"
// xpath: //valuta[@category='EUR']/name
// This returns each name node where category="EUR"
// xpath: //valuta[@category='EUR']
// This returns each valuta node with children where category="EUR"
string xpath = "//valuta[@category='EUR']/value";
XPathExpression expr = nav.Compile(xpath);
XPathNodeIterator iterator = nav.Select(expr);
while (iterator.MoveNext())
{
Console.WriteLine(iterator.Current.InnerXml);
}
Cheers,
Dave