I am trying to create an xml doc by serializing objects. I came across an issue.
Target xml structure is similar to an HTML page. It has a form element with some attributes
and can have any number of controls like textfields, buttons etc. Object I create for this structure is given below. To add all these controls I am using an array list called items. When the object is seraialzed all the controls appear inside the tags . I want the controls appear as the direct child of form elements. How can I do this?
[XmlInclude(typeof(Lstatic))]
[XmlInclude(typeof(textField))]
public class form
{
[XmlAttribute]
public String action
{
get;
set;
}
[XmlAttribute]
public String method
{
get;
set;
}
[XmlAttribute]
public String name
{
get;
set;
}
[XmlArray]
public ArrayList items
{
get;
set;
}
}
and this is the resulting XML
<form name="login" method="get" action="/FetchIndex.asmx/findAddresses">
<items>
<anyType value="Please key in your details:" xsi:type="Lstatic"/>
<anyType name="postCode" value="" xsi:type="textField" size="10" label="Postcode:" hint="Enter your postcode"/>
</items>
</form>
Instead I want the resulting xml like this
<form name="login" method="get" action="/FetchIndex.asmx/findAddresses">
<anyType value="Please key in your details:" xsi:type="Lstatic"/>
<anyType name="postCode" value="" xsi:type="textField" size="10" label="Postcode:" hint="Enter your postcode"/>
</form>
How can i do this in c#?
Thanks
This code is untested, sorry, it is only what I remember. I believe you need the
XmlElementattribute.If it works, there’s a possibility it might also work without the need of the
itemsSerializableproperty, you should check it out.