[英]Extracting data from XML into a Dictionary C#
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.我有以下 XML 文件:带有字典键和值的 XML 文件我必须提取以下数据,正如您在照片上看到的那样,并将数据保存到字典中。 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.如果您能帮我提取数据并给我一些如何将其保存到字典中的想法,我将非常感激。
Using Xml Linq :使用 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.