Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have the following XML part which schema I can't change. NUMBER, REGION, MENTION, FEDERAL are columns:

<COLUMNS LIST="20" PAGE="1" INDEX="reg_id">
  <NUMBER WIDTH="3"/>
  <REGION WIDTH="60"/>
  <MENTION WIDTH="7"/>
  <FEDERAL WIDTH="30"/>
</COLUMNS>

I want to deserialize it to public List<Column> Columns {get;set;} property. So element name would go to Column.Name. Column class:

public class Column
{
   //Name goes from Element Name
   public string Name {get;set;}
   [XmlAttribute("WIDTH")]
   public int Width {get;set;}
}

Is it possible with XmlSerializer class?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
402 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can use multiple XmlElements with one property:

[XmlElement("NUMBER")]
[XmlElement("REGION")]
[XmlElement("MENTION")]
[XmlElement("FEDERAL")]
public List<Column> Columns {get;set;}

It is even possible to specify different classes for different tag names:

[XmlElement(ElementName = "One", Type = typeof(OneItem))]
[XmlElement(ElementName = "Two", Type = typeof(TwoItem))]
public List<BaseItem> Items {get;set;}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...