Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML string array deserialize as different type name

I have the following C# class property:

private List<string>  _accountTypes;

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

which is initialized like this in the class constructor:

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

When deserialized I get this:

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

I should like it if I could get this:

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

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

like image 972
Moe Howard Avatar asked Feb 25 '11 01:02

Moe Howard


1 Answers

I think you are searching for the [XmlArrayItem] Attribute.

Try this:

[XmlArray(ElementName = "accountTypes")]
[XmlArrayItem("accountType")]
public List<string> AccountTypes
{
   get { return _accountTypes; }
   set { _accountTypes = value; }
}
like image 184
nhu Avatar answered Oct 10 '22 09:10

nhu