[SOLVED] Extracting data from XML into a Dictionary C#

Issue

everyone!

I have the following XML file:
XML file with key and value for the dictionary
And I have to extract the following data, as you can see on the photo and to save the data into a dictionary. Until now I have the class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;

[XmlRoot("Value")]

//Here is the class:
public class CodeListParser
{
    public string simpleValueCode { get; set; }
    public string simpleValueName { get; set; }

    public CodeListParser()
    {
        this.simpleValueCode = simpleValueCode;
        this.simpleValueName = simpleValueName;
    }

    public Dictionary<string, string> GetRulePlanRsa()
    {

        Dictionary<string, string> rulePlanRsa = new Dictionary<string, string>();
        using (TextReader reader = new StreamReader(@"C:\Users\mtodorova\source\repos\XMLProject\XMLProject\bin\Debug\net6.0\C17000608_genericode.xml"))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(CodeListParser));
           //return (CodeListParser)serializer.Deserialize(reader);
        }
        return rulePlanRsa;

    }
    //The Deserialize function:

    public static Dictionary<string, string> GetLegalForms()
    {

        Dictionary<string, string> rulePlanLegalForms = new Dictionary<string, string>();
        using (TextReader reader = new StreamReader(@"C:\Users\mtodorova\source\repos\XMLProject\XMLProject\bin\Debug\net6.0\C60000022_genericode.xml"))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(CodeListParser));
            //return (CodeListParser)serializer.Deserialize(reader);
        }
        return rulePlanLegalForms;
    }

}

And in the main method I am trying to extract the data, unfortunately without success:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;

class Program
{
    public static void Main()
    {        
         try
         {
             XmlDocument doc = new XmlDocument();
             doc.Load(@"C:\Users\mtodorova\source\repos\XMLProject\XMLProject\bin\Debug\net6.0\C17000608_genericode.xml");

             XmlNodeList nodes = doc.SelectNodes("//SimpleCodeList/Row/Value/SimpleValue");
             List<CodeListParser> values = new List<CodeListParser>();
             foreach (XmlNode xmlNode in nodes)
             {
                 CodeListParser value = new CodeListParser();
                 value.simpleValueCode = xmlNode["code"].InnerText;
                 value.simpleValueName = xmlNode["name"].InnerText;
                 
                values.Add(value);
             }
            Console.WriteLine(values);
         }
         catch
         {
             throw;
         }
    }
}

As Output I am receiving and it doesn’t work the way I want it to:

System.Collections.Generic.List`1[CodeListParser]

I would be very thankful, if you can help me extract the data and give me some ideas how to save it into a dictionary.

Solution

Using Xml Linq :

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


namespace ConsoleApplication23
{

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

            Dictionary<string, string> rulePlanRsa = doc.Descendants("Value")
                .GroupBy(x => (string)x.Attribute("ColumnRef"), y => (string)y.Element("SimpleValue"))
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());
        }
    }

 
}

Answered By – jdweng

Answer Checked By – Dawn Plyler (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.