Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将此字典像XML一样反序列化到字典<;字符串,TValue>;?_C#_Xml_Dictionary_Generics_Serialization - Fatal编程技术网

C# 如何将此字典像XML一样反序列化到字典<;字符串,TValue>;?

C# 如何将此字典像XML一样反序列化到字典<;字符串,TValue>;?,c#,xml,dictionary,generics,serialization,C#,Xml,Dictionary,Generics,Serialization,我有以下materials.xml文件: 1000 沙 灰尘 #faff 2000 水 #1CA3EC64 液体 #5F6A0032 3000 气体 气体 单个材质用以下类别表示: 公共类材料{ 公共只读int-id; 公共只读字符串名称; 公共色彩; 公共材料状态; } 我需要一个通用的SerializableDictionary类,可以这样使用: var materials=new SerializableDictionary(); //…序列化的东西 材料[“岩石”]。颜色; 这是

我有以下
materials.xml
文件:


1000
沙
灰尘
#faff
2000
水
#1CA3EC64
液体
#5F6A0032
3000
气体
气体
单个材质用以下类别表示:

公共类材料{
公共只读int-id;
公共只读字符串名称;
公共色彩;
公共材料状态;
}
我需要一个通用的
SerializableDictionary
类,可以这样使用:

var materials=new SerializableDictionary();
//…序列化的东西
材料[“岩石”]。颜色;
这是可以实现的吗?

使用xml linq:

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

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            
            List<Material> materials = doc.Root.Elements()
                .Select(x => new Material()
                {
                    id = (int)x.Element("id"),
                    name = (string)x.Element("name"),
                    color =  Color.FromArgb(int.Parse(((string)x.Element("color")).Substring(1), NumberStyles.HexNumber)),
                    state = (MaterialState)Enum.Parse(typeof(MaterialState),(string)x.Element("state"))
                }).ToList();

            Dictionary<string, Material> dict = materials
                .GroupBy(x => x.name, y => y)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());

        }
    }
    public enum MaterialState
    {
        Dust,
        Liquid,
        Gas
    }
    public class Material
    {
        public int id { get;set; }
        public string name { get;set;}
        public Color color { get;set;}
        public MaterialState state { get;set;}
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
使用系统图;
利用制度全球化;
命名空间控制台应用程序1
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Load(文件名);
列表物料=doc.Root.Elements()
.选择(x=>新材料()
{
id=(int)x.Element(“id”),
名称=(字符串)x.Element(“名称”),
color=color.FromArgb(int.Parse(((string)x.Element(“color”))).Substring(1),NumberStyles.HexNumber)),
state=(MaterialState)Enum.Parse(typeof(MaterialState),(string)x.Element(“state”))
}).ToList();
字典dict=材料
.GroupBy(x=>x.name,y=>y)
.ToDictionary(x=>x.Key,y=>y.FirstOrDefault());
}
}
公共枚举材质状态
{
灰尘
液体
气体
}
公开课材料
{
公共int id{get;set;}
公共字符串名称{get;set;}
公共颜色{get;set;}
公共材料状态{get;set;}
}
}

这很有用,但我想我可以通过一个通用类来实现这一点,该类可以将
中每个元素的内容转换为
材料。也许用例如
元素替换
元素有助于推广序列化逻辑?为此,我尝试使用
XmlReader
,但它似乎无法将元素的内容转换为类(或者很可能是我做错了什么)。我认为您需要一个基类材质,其中包含继承的类Sand、Water、Gas。不管您是使用XmlReader还是XMLLINQ解析文件,您仍然需要三个继承的类。