将XML属性反序列化为Dictionary <string,object =“”>

时间:2015-07-08 14:40:48

标签: c# xml dictionary serialization deserialization

假设我正在尝试反序列化以下XML(在C#程序中):

<Data>
    <DataItem Id="1" Attrib1="Val1_1" Attrib2="Val2_1" Attrib3="Val3_1" />
    <DataItem Id="2" Attrib1="Val1_2" Attrib4="Val4_2" />
    <DataItem Id="3" Attrib3="Val3_1" Attrib5="Val5_3" />
</Data>

因此,每个DataItem元素都会有一个Id字段和一组随机的属性,我想要捕获/存储。

我想象最终的包含类可能如下所示:

[XmlRootAttribute("Data")]
public class Data
{
    [XmlElement("DataItem")]
    public DataItem[] Items { get; set; }
}

public class DataItem
{
    public Dictionary<string, object> Vals { get; set; }

    [XmlIgnore]
    public string Id { get { return (string)Vals[Id]; } }
}

(如果我在这个结构上错了,请告诉我 - 这对我来说仍然是新鲜的!!)

但我不确定如何将属性反序列化为我的字典的[Key][Value]对。 我发现this questionthis other answer似乎指向了正确(虽然不同)的方向,但我在如何正确实现这一点方面迷失了,并且知道这应该相当容易。

关于如何做到这一点的任何帮助/链接/样本都将非常感激!!

感谢!!!

3 个答案:

答案 0 :(得分:1)

更通用的解决方案是:

public class DynamicAttributes : IXmlSerializable, IDictionary<string, object>
{
    private readonly Dictionary<string, object> _attributes;

    public DynamicAttributes()
    {
        _attributes = new Dictionary<string, object>();
    }

    public XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(XmlReader reader)
    {
        if (reader.HasAttributes)
        {
            while (reader.MoveToNextAttribute())
            {
                var key = reader.LocalName;
                var value = reader.Value;

                _attributes.Add(key, value);
            }

            // back to the owner of attributes
            reader.MoveToElement();
        }

        reader.ReadStartElement();
    }

    public void WriteXml(XmlWriter writer)
    {
        foreach (var attribute in _attributes)
        {
            writer.WriteStartAttribute(attribute.Key);

            writer.WriteValue(attribute.Value);

            writer.WriteEndAttribute();
        }
    }

    // implementation of IDictionary<string, object> comes here
}

与您的案例相比,班级是:

[XmlRoot("Data")]
public class Data
{
    [XmlElement("DataItem")]
    public DataItem[] Items { get; set; }
}

public class DataItem : DynamicAttributes
{
    [XmlIgnore]
    public string Id
    { 
        get
        {                
            return this.TryGetValue("Id", out var value) ? value.ToString() : null;
        } 
    }
}

答案 1 :(得分:0)

好的,正如我怀疑的那样(并在评论中提出),我放弃了直接的XML序列化并编写了我自己的解析方法来实现这一目标。

对于其他任何人来说,这可能会有所帮助,这就是我最终写的内容 - 希望它也能帮到你(如果有人有更好的答案,请发布吧! - 我很想知道如何才能实现这一目标/做得更好):

public class Data
{
    public List<DataItem> Items { get; set; }

    private Data(XElement root)
    {
        Items = new List<DataItem>();

        foreach (XElement el in root.Elements())
        {
            Items.Add(new DataItem(el));
        }
    }

    public static Data Load(Stream stream)
    {
        return new Data(XDocument.Load(stream).Root);
    }
}

public class DataItem
{
    public Dictionary<string, string> Vals;
    public string Id { get { return (string)Vals["Id"]; } }

    public DataItem(XElement el)
        {
            Vals = new Dictionary<string, string>();

            // Load all the element attributes into the Attributes Dictionary
            foreach (XAttribute att in el.Attributes())
            {
                string name = att.Name.ToString();
                string val = att.Value;

                Vals.Add(name, val);
            }
        }
}

然后它将通过以下方式在代码中使用:

Data MyData = Data.Load(MyXMLStream);

希望这也有助于其他人!!

答案 2 :(得分:-2)

使用此

<Data>
    <DataItem Id="1" Val="Val1_1" />
    <DataItem Id="1" Val="Val2_1" />
    <DataItem Id="1" Val="Val3_1" />
    <DataItem Id="2" Val="Val1_2" />
    <DataItem Id="2" Val="Val4_2" />
    <DataItem Id="3" Val="Val3_1" />
    <DataItem Id="3" Val="Val5_3" />
</Data>