0

I have a big ton of xml-data with a lot of properties and I want to parse the values out of the xml-lines that contain specific properties. This is the xml data:

<Root xmlns:wb="http://www.worldbank.org">
  <data>
    <record>
      <field name="Country or Area" key="ABW">Aruba</field>
      <field name="Item" key="SP.URB.TOTL.IN.ZS">Urban population (% of total)</field>
      <field name="Year">1960</field>
      <field name="Value">50.776</field>
    </record>
 </data>
</Root>

I would like to get Aruba, 1960 and 50.776 out of this.

I've tried this:

XmlDocument xml = new XmlDocument();
              xml.Load("daten.xml");
XmlNodeList list = xml.SelectNodes("//data/record/field");
            foreach (XmlNode item in list)
            {
                Console.WriteLine(item.Attributes["Name"].Value);
            }

This one throws an exception, but I tried also other ways with item["field"] or item["field@[name='Year']], but nothing worked out for me.

actopozipc
  • 93
  • 12
  • You said _"This one throws an exception"_ - what exception exactly? – vasily.sib Apr 15 '19 at 04:24
  • System.NullReferenceException – actopozipc Apr 15 '19 at 04:35
  • Oh, then you should look [here](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) and debug your code. – vasily.sib Apr 15 '19 at 04:37
  • XmlNode.Attributes indexer will return null if the specified attribute is not present on that node. Bear in mind that attribute names are case sensitive... – T2PS Apr 15 '19 at 04:39
  • You might want to consider using LINQ to XML. https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/linq-to-xml-overview –  Apr 15 '19 at 04:46
  • Thanks, still doesnt give me the output I wanted. Now it returns "Item", "Year" and "Value" – actopozipc Apr 15 '19 at 04:59

1 Answers1

0

Here is one way using xml linq :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication108
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            List<Record> records = new List<Record>();
            foreach (XElement record in doc.Descendants("record"))
            {
                Record newRecord = new Record();
                records.Add(newRecord);
                foreach (XElement field in record.Elements("field"))
                {
                    string name = (string)field.Attribute("name");

                    switch (name)
                    {
                        case "Country or Area":
                            newRecord.country_area_key = (string)field.Attribute("key");
                            newRecord.country_area_name = (string)field;
                            break;

                        case "Item":
                            newRecord.item_key = (string)field;
                            break;

                        case "Year":
                            newRecord.year = (int)field;
                            break;

                        case "Value":
                            newRecord.value = (decimal)field;
                            break;
                    }
                }
            }

        }
    }
    public class Record
    {
        public string country_area_key { get;set;}
        public string country_area_name { get;set;}
        public string item_key { get;set;}
        public int year { get;set;}
        public decimal value { get;set;}
    } 

}
jdweng
  • 33,250
  • 2
  • 15
  • 20