简体   繁体   中英

Deserializing xml attributes

I'm using XmlSerializer to deserialize an xml document into some class objects. 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.
    • Call the mapping function passing it the temporary variable 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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