开发者

Complex XML Serialization Question

So, I'm trying to create a specific XML format for the consumers of an API I'm creating. Effectively, you would have a model like so:

public class SampleModel
{
     public ErrorCodeEnum ErrorCode { get; set; }
     public List<Error> LocalizedErrors { get; set; }
     public object AdditionalInformation { get; set; }
}

Now lets say I have a sample model like so:

SampleModel model = new SampleModel()
model.ErrorCode = ErrorCodeEnum.InvalidParameters;
model.LocalizedErrors = new List<Error>
{
    new Error { For = "SomeField", Message = "Your SomeField value is unacceptable." },
    new Error { For = "SomeField2", Message = "Your SomeField2 value is unacceptable." },
};
model.AdditionalInformation = new SomeOtherType { SomeProperty = "Whatever" };

I'd want to make sure that serializes into something like this:

<xml>
<SampleModel>
   <ErrorCode>InvalidParameters</ErrorCode>
   <Errors>
       <Error For="Somefield">Your SomeField value is unacceptable.</Error>
       <Error For="Somefield2">Your SomeField2 value is unacceptable.</Error>
   </Errors>
   <SomeProperty>Whatever</SomeProperty>
</SampleModel>

I've solved a fair amount of this problem, but one stickler remains. Basically, right now, it all serializes out like so:

<xml>
<SampleModel>
   <ErrorCode>InvalidParameters</ErrorCode>
   <Errors>
       <Error For="Somefield">Your SomeField value is unacceptable.</Error>
       <Error For="Somefield2">Your SomeField2 value is unacceptable.</Error>
   </Errors>
   <AdditionalInformation>
       <SomeProperty>Whatever</SomeProperty>
   </AdditionalInformation>
</SampleModel>

The default XmlSerializer is outputting the name of the 开发者_JAVA技巧property (in this case AdditionalInformation) around the serialized nodes of it's value. I've been trying multiple ways to get rid of this node. What I'd like to see is basically my dictionary of values to be appended at the 1 child deep level of the XML Root, and get rid of the AdditionalInformation. So if I had a model like so:

SampleModel model = new SampleModel()
model.ErrorCode = ErrorCodeEnum.InvalidParameters;
model.LocalizedErrors = new List<Error>
{
    new Error { For = "SomeField", Message = "Your SomeField value is unacceptable." },
    new Error { For = "SomeField2", Message = "Your SomeField2 value is unacceptable." },
};
model.AdditionalInformation = new AThirdType { Foo = "Bar", Bing = "Baz" };

I'd get the following XML:

<xml>
<SampleModel>
   <ErrorCode>InvalidParameters</ErrorCode>
   <Errors>
       <Error For="Somefield">Your SomeField value is unacceptable.</Error>
       <Error For="Somefield2">Your SomeField2 value is unacceptable.</Error>
   </Errors>
   <Foo>Bar</Foo>
   <Bing>Baz</Bing>
</SampleModel>

Is this possible without going through the effort of making my own XmlSerializer / Deserializer?


I am afraid that this is not possible without implementing some custom serialization mechanism (like IXmlSerializable) because imagine that you had this XML and you wanted to deserialize it back to a model. The serializer cannot possibly know that those properties belong to the complex object.

Another strategy that you could adapt is to simply create a view model class which will be adapted to the desired XML result and then use AutoMapper to map and flatten your model to this view model.

Yet another possibility is to write your custom serialization mechanism using directly XDocument.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜