-2

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.

mariatod
  • 3
  • 2
  • welcome to stackoverflow mariatod. i do curious, is the xml really.. broken without closing tags? can you describe your "unsuccessful attempt" more? did you hit an error or something? that `System.Collections.Generic.List\`1[CodeListParser]` meant you are trying to `Console.Write` a `List` instead of trying to print its contents. – Bagus Tesa Jul 19 '22 at 11:46
  • 1
    Does [these answers](https://stackoverflow.com/questions/759133/how-to-display-list-items-on-console-window-in-c-sharp) help your question in printing `List`? – Bagus Tesa Jul 19 '22 at 11:50
  • Please [edit] your question to include your XML as **text** rather than as a screenshot. On stack overflow images should not be used for textual content, see [*Discourage screenshots of code and/or errors*](https://meta.stackoverflow.com/a/307500) and [*Why not upload images of code on SO when asking a question*](https://meta.stackoverflow.com/a/285557) for why. For instructions on formatting see *[How do I format my code blocks?](https://meta.stackexchange.com/q/22186)*. A [mcve] showing what you have tried that did not work would maximize your chances of getting help. See [ask]. – dbc Jul 19 '22 at 14:16

1 Answers1

0

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());
        }
    }

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