XML array array is deserialized as another type name

I have the following C # property:

private List<string> _accountTypes; [XmlArray(ElementName = "accountTypes")] public List<string> AccountTypes { get { return _accountTypes; } set { _accountTypes = value; } } 

which is initialized as follows in the constructor of the class:

 _accountTypes = new List<string>( new string[] { "OHGEE", "OHMY", "GOLLY", "GOLLYGEE" }); 

When deserializing, I get the following:

 <accountTypes> <string>OHGEE</string> <string>OHMY</string> <string>GOLLY</string> <string>GOLLYGEE</string> </accountTypes> 

I would like if I could do this:

 <accountTypes> <accountType>OHGEE</accountType> <accountType>OHMY</accountType> <accountType>GOLLY</accountType> <accountType>GOLLYGEE</accountType> </accountTypes> 

Without creating a subclass like "accountType", how can this be done? Are there any XML attribute properties that can be used to get what I need?

+6
c # xml deserialization xml-serialization
source share
1 answer

I think you are looking for the attribute [XmlArrayItem] .

Try the following:

 [XmlArray(ElementName = "accountTypes")] [XmlArrayItem("accountType")] public List<string> AccountTypes { get { return _accountTypes; } set { _accountTypes = value; } } 
+11
source share

All Articles