开发者

Flatten a collection during serialization

I'm trying to convert a "collection of collections" into better looking xml. Basically, I want to lie to the service consumer and make it look like there's a real object.

This is what WCF creates automatically

<EntityPropertyCollection xmlns="http://schemas.datacontract.org/2004/07/CustomSerializer" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
 <EntityProperty>
  <Name>Test</Name>
  <Value i:nil="true"/>
 </EntityProperty>
 <EntityProperty>
  <Name>Test2</Name>
  <Value i:type="EntityPropert开发者_运维问答yCollection">
   <EntityProperty>
    <Name>Nested1</Name>
    <Value i:nil="true"/>
   </EntityProperty>
   <EntityProperty>
    <Name>Nested2</Name>
    <Value i:nil="true"/>
   </EntityProperty>
  </Value>
 </EntityProperty>
</EntityPropertyCollection>

This is what I'd like to achieve

<Something>
 <Test i:nil="true"/>
 <Test2 i:type="Something">
  <Nested1 i:nil="true"/>
  <Nested2 i:nil="true"/>
 </Test2>
</Something>

A custom serializer for a particular type (EntityProperyCollection) would be good, but other posts indicate that's not an option.

I looked at Data Contract Surrogates. I think I could generate a custom type and allow the DataContractSerializer to serialize the generated type. But, I'm hoping there's a simpler solution.


You could implement IXmlSerializable on your return object and do the serialization manually, or just return stream and use an XmlWriter to create exactly the XML you want.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜