C# 当字典具有不同的类型时,我可以从XML数据创建类型良好的字典吗?
我希望将以下XML信息转换为一组字典。每个C# 当字典具有不同的类型时,我可以从XML数据创建类型良好的字典吗?,c#,xml,types,xml-deserialization,C#,Xml,Types,Xml Deserialization,我希望将以下XML信息转换为一组字典。每个描述一个字典,每个描述一个键值对。这是为了在运行时反序列化,因此使序列化变得困难的解决方案并非不可能 <Config> <Transform Input="System.String" Output="System.Boolean"> <Map Input="Yes" Output="true"></Map> <Map Input="No" Output="false">&l
描述一个字典,每个
描述一个键值对。这是为了在运行时反序列化,因此使序列化变得困难的解决方案并非不可能
<Config>
<Transform Input="System.String" Output="System.Boolean">
<Map Input="Yes" Output="true"></Map>
<Map Input="No" Output="false"></Map>
</Transform>
<Transform Input="System.String" Output="System.Int32">
<Map Input="Category1" Output="1"></Map>
<Map Input="Category2" Output="2"></Map>
<Map Input="Category3" Output="3"></Map>
<Map Input="Category4" Output="4"></Map>
</Transform>
</Config>
这是我第一次尝试将这些类型反序列化为。数据被正确地反序列化到这些类中,因此剩下的唯一步骤是将其转换为字典
[XmlRoot]
public class Config
{
[XmlElement("Transform")]
public List<Transform> Transforms { get; set; }
}
public class Transform
{
[XmlIgnore]
private Type inputType { get; set; }
[XmlIgnore]
private Type outputType { get; set; }
[XmlAttribute]
public string Input {
get { return inputType.AssemblyQualifiedName; }
set
{
inputType = Type.GetType(value, true);
}
}
[XmlAttribute]
public string Output
{
get { return outputType.AssemblyQualifiedName; }
set
{
outputType = Type.GetType(value, true);
}
}
[XmlElement("Map")]
public List<Map> Mappings { get; set; }
}
public class Map
{
[XmlAttribute]
public string Input { get; set; }
[XmlAttribute]
public string Output { get; set; }
}
[XmlRoot]
公共类配置
{
[XmlElement(“转换”)]
公共列表转换{get;set;}
}
公共类转换
{
[XmlIgnore]
私有类型inputType{get;set;}
[XmlIgnore]
私有类型输出类型{get;set;}
[XmlAttribute]
公共字符串输入{
获取{return inputType.AssemblyQualifiedName;}
设置
{
inputType=Type.GetType(值,true);
}
}
[XmlAttribute]
公共字符串输出
{
获取{return outputType.AssemblyQualifiedName;}
设置
{
outputType=Type.GetType(值,true);
}
}
[XmlElement(“映射”)]
公共列表映射{get;set;}
}
公共类地图
{
[XmlAttribute]
公共字符串输入{get;set;}
[XmlAttribute]
公共字符串输出{get;set;}
}
我最初的策略是找到一种使用System.Type对象的方法,在transform类中添加一个函数,该函数将返回相应的字典,但我能学到的最好方法是返回一个字典
- 也许有某种方法可以使
转换
类通用
- 也许
Dictionary
不是正确的目标,但是其他一些结构会更适合吗?(这些词典的使用者希望第一次转换为IEnumerable
)
编辑:最终目标是为XML中的第一次转换创建一个
字典,并创建一个
字典。请尝试以下操作:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
Dictionary<Type, Dictionary<Type, List<KeyValuePair<string, Boolean>>>> dict = doc.Descendants("Transform")
.GroupBy(x => Type.GetType((string)x.Attribute("Input")), y => y)
.ToDictionary(x => x.Key, y => y.GroupBy(a => Type.GetType((string)a.Attribute("Output")), b => b.Elements("Map").Select(c => new KeyValuePair<string, Boolean>((string)c.Attribute("Input"), (string)c.Attribute("Output") == "true" ? true : false)).ToList())
.ToDictionary(a => a.Key, b => b.FirstOrDefault()));
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序1
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Load(文件名);
Dictionary dict=文档子体(“转换”)
.GroupBy(x=>Type.GetType((字符串)x.Attribute(“输入”)),y=>y)
.ToDictionary(x=>x.Key,y=>y.GroupBy(a=>Type.GetType((string)a.Attribute(“Output”)),b=>b.Elements(“Map”).Select(c=>newkeyvaluepair((string)c.Attribute(“Input”),(string)c.Attribute(“Output”)==true?true:false)).ToList())
.ToDictionary(a=>a.Key,b=>b.FirstOrDefault());
}
}
}
这为我提供了一组对,但我的最终目标是为第一次变换获得对,为第二次变换获得对。我的缺点是没有把这说得更清楚;我将对问题进行编辑,使其更加明确。