A
A
Alexey Smirnov2015-12-24 00:48:02
.NET
Alexey Smirnov, 2015-12-24 00:48:02

How, in an XML document, using the .NET framework, to get data from the text node of some element that has a certain attribute?

Hello.
There is some XLM code:

<ROOT>
    <data>
        <record>
            <field name="Country or Area">Australia</field>
            <field name="Year">2012</field>
            <field name="Value">0.411643502534169</field>
            <field name="Value Footnotes"></field>
        </record>
        <record>
            <field name="Country or Area">Russia</field>
            <field name="Year">2011</field>
            <field name="Value">0.427343851420981</field>
            <field name="Value Footnotes"></field>
        </record>
        <record>
            <field name="Country or Area">Spain</field>
            <field name="Year">2010</field>
            <field name="Value">0.438586168922878</field>
            <field name="Value Footnotes"></field>
        </record>
    </data>
</ROOT>

From this XML code, I need to get text data from all 3 "field" node elements with the "Country or Area" attribute.
How, using the .NET framework, to extract string data from the text nodes of "field" elements with the attribute "Country or Area"?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2015-12-24
@ERAFY

Here is an example in C #, I think I can rewrite it in VB without any problems. For the purpose of searching for anything in XML, there is XPATH.

using System;
using System.Linq;
using System.Xml.Linq;
using System.Xml.XPath;
          
public class Program
{
  public static void Main()
  {
    var xml = XDocument.Load("test.xml");
    
    xml.XPathSelectElements("//field[@name='Country or Area']").ToList().ForEach(n =>
      Console.WriteLine(n.Value)
    );
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question