Reputation: 31
If I have an object like so:
[XmlRoot("Person")]
public class Person
{
[XmlElement("LastName")]
public string Last {get;set;}
[XmlElement("FirstName")]
public string First {get;set;}
[XmlElement("Banners")]
public List<Banner> Banners {get;set;}
}
[XmlType("Banner")]
public class Banner
{
[XmlElement("Title")]
public string Title {get;set;}
[XmlElement("Location")]
public string Location {get;set;}
}
When serializing it locally, it turns out to look ok
<Person>
<LastName/>
<FirstName/>
<Banners>
<Banner>
<Title/>
<Location/>
</Banner>
</Banners>
</Person>
But if create a library and host it from a WCF IIS service, and I want to use it from a client application, when I access the Person object, fill it with my parameters and then serialize it to an xml string I get this:
<Person>
<Banners>
<Banner>
<Location/>
<Title/>
</Banner>
</Banners>
<FirstName/>
<LastName/>
</Person>
all the nodes are shown in alphabetical, per level after the root. I have tried using the Order keyword in the XmlElement Attribute, but it doesn't see it apparantly.
Now if I use the object locally and serialize it, then it adheres to the Order keyword. What am I doing wrong?
Thanks
Upvotes: 2
Views: 274
Reputation: 3380
Is XmlSerializer mandatory?
WCF is using the DataContractSerializer by default.
So I would switch to that.
The attribute is DataMember
- instead of XmlElement
.
With DataMember you can add an order value, that will be honored by the DataContract Serializer.
[DataMember(Order = 0)]
public string FirstName { get; set; }
[DataMember(Order = 1)]
public string LastName { get; set; }
[DataMember(Order = 2)]
public string Email { get; set; }
[DataMember(Order = 3)]
public string Password { get; set; }
Upvotes: 1