【问题标题】:Can I create well-typed dictionaries from XML data when the dictionaries have different types?当字典具有不同类型时,我可以从 XML 数据创建类型良好的字典吗?
【发布时间】:2020-05-22 18:58:18
【问题描述】:

我希望将以下 XML 信息转换为字典集合。每个<Transform> 描述一个字典,每个<Map> 描述一个键值对。这旨在在运行时进行反序列化,因此使序列化变得困难的解决方案并非不可能。

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

我最初的策略是找到一种方法来使用 System.Type 对象向转换类添加一个函数,该函数将返回适当的字典,但我能学到的最好方法是返回一个 Dictionary&lt;object, object&gt;

  • 也许有一些方法可以使 Transform 类通用?
  • 也许Dictionary&lt;T, U&gt; 不是正确的目标,但其他一些结构更适合? (这些字典的使用者会期望 IEnumerable&lt;KeyValuePair&lt;string, bool&gt;&gt; 进行第一次转换)

编辑:最终目标是为 XML 中的第一个转换创建一个 &lt;string, bool&gt; 字典和一个 &lt;string, int&gt; 字典。

【问题讨论】:

    标签: c# xml types xml-deserialization


    【解决方案1】:

    尝试以下:

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

    【讨论】:

    • 这给了我一组 对,但我的最终目标是获得第一个转换的 对和第二个转换的 对。没有说得更清楚是我的坏事;我将编辑问题以使其更明确。
    猜你喜欢
    • 2010-10-13
    • 2022-06-22
    • 1970-01-01
    • 2020-11-05
    • 1970-01-01
    • 2020-10-20
    • 1970-01-01
    相关资源
    最近更新 更多