ServiceStack.Text.XmlSerializer.DeserializeFromString result change when I change the order of xmlnode. Why?

StackOverflow https://stackoverflow.com/questions/18230417

سؤال

What is wrong with ServiceStack.Text.XmlSerializer ?

I have object:

public class weatherdata : IReturn<WashService>
{
    public Location location { get; set; }
}
public class Location
{
    public string name { get; set; }
    public string country { get; set; }
}

Try to deserialize thirdparty xml like that:

var data = ServiceStack.Text.XmlSerializer.DeserializeFromString<weatherdata>("<weatherdata><location><name>Moscow</name><country>RU</country></location></weatherdata>");

data.location.name = Moscow. data.location.country is NULL;

Change xml like that:

var data = ServiceStack.Text.XmlSerializer.DeserializeFromString<weatherdata>("<weatherdata><location><country>RU</country><name>Moscow</name></location></weatherdata>");

and see

data.location.name == "Moscow".
data.location.country =="RU";

Why so different results if I only change order?

هل كانت مفيدة؟

المحلول

As explained here, the default XML serializer used by ServiceStack (.NET's DataContract serializer) assumes that the XML elements must be in the same order as declared in your class. In XML schema terminology, the elements are declared as xs:sequence rather than xs:all. If you need to support XML elements in any possible ordering in the request, then you may need to override the XML serializer used by ServiceStack as explained in the link above.

If you just need to adjust the ordering of the XML elements, I believe you can specify an exact ordering for your elements by decorating your properties with DataMember attributes and specifying the Order property. If you do this, then you will also need to decorate your Location class with a DataContract attribute.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top