简体   繁体   English

反序列化xml属性

[英]Deserializing xml attributes

I'm using XmlSerializer to deserialize an xml document into some class objects. 我正在使用XmlSerializer将xml文档反序列化为某些类对象。 Is there a way to deserialize a node's attributes into a dictionary of key/values rather than having to name properties for each attribute? 有没有一种方法可以将节点的属性反序列化为键/值的字典,而不必为每个属性命名属性? Example: 例:

public class Panel {

    public Dictionary<string, string> AllAttributes {get;set;}

    [XmlElement("image", typeof(Image))]
    [XmlElement("panel", typeof(Panel))]
    public object[] Items { get; set; }

}

You have two options: 您有两种选择:

  1. Implement your own serializer. 实现自己的序列化器。

  2. Implement a mapping function: 实现映射功能:

    • Use XmlSerializer to deserialize to a temporary variable (eg var temp ) instead of directly into your class. 使用XmlSerializer反序列化为临时变量(例如var temp ),而不是直接反序列化到您的类中。
    • Call the mapping function passing it the temporary variable temp . 调用传递给它的映射函数临时变量temp This function creates a new instance of your Panel class, then loops through the temp properties and maps them to this instance, and finally it returns the new instance. 此函数创建Panel类的新实例,然后遍历temp属性并将其映射到该实例,最后返回新实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM