繁体   English   中英

将数据从 XML 提取到字典 C#

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

每个人!

我有以下 XML 文件:带有字典键和值的 XML 文件我必须提取以下数据,正如您在照片上看到的那样,并将数据保存到字典中。 直到现在我有课:

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

}

在我试图提取数据的主要方法中,不幸的是没有成功:

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

作为我收到的输出,它不能按我想要的方式工作:

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

如果您能帮我提取数据并给我一些如何将其保存到字典中的想法,我将非常感激。

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

 
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM