反序列化包含Dictionary的XML

时间:2016-09-08 16:12:43

标签: c# .net xml dictionary

我有一个XML文件,其中包含我想要的标识符,xml看起来像

cr1 = int(input("What is the convertion rate of the first one? "))

我想要的标识符是60371408,60371409

3 个答案:

答案 0 :(得分:1)

我刚刚找到了解决方案: enter image description here

感谢您的反应:D

答案 1 :(得分:0)

创建XmlSerializer

var serializer = new XmlSerializer(typeof(Dictionary<string, object>));

...然后反序列化。例如,您还没有指定此XML的格式,无论您是字符串还是流。以下是您对XML字符串进行反序列化的方法:

var reader = new StringReader(xml);
var dict = serializer.Deserialize(reader);

现在您拥有Dictionary<string, object>,但由于值是普通对象,因此您需要转换值:

var list = (List<int>)dict["key"];

答案 2 :(得分:0)

试试xml linq

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

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

            XElement dictionary = doc.Descendants().Where(x => x.Name.LocalName == "Dictionary").FirstOrDefault();
            XNamespace xNs = dictionary.GetNamespaceOfPrefix("x");

            var results = dictionary.Descendants(xNs + "Int32").Select(x => (int)x).ToList();

        }

    }
}
相关问题