Skip to content

Xml attribute values

Hrvoje

Asked Jul 15, 2009, 11:04 PM EDT1 answer
Share
LinkedInX
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" ?
 

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Hi,

    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
    0