开发者

Change the XML Serializer's element name of an array element

开发者 https://www.devze.com 2022-12-20 07:07 出处:网络
I have a class which is generated from an XML file via the XSD.exe tool.The class I have contains an array with elements.

I have a class which is generated from an XML file via the XSD.exe tool. The class I have contains an array with elements.

Until recently开发者_StackOverflow社区, rendering the whole document from a fully instantiated business object was possible, however due to the size, we now need to render the documents array elements to a stream so that we don't run out of memory.

However when you render the array elements you get a different element name in the XML serialization. I tried to create an XMLAttributesOverride but this returned me an error stating that I could not override the XmlElement attributes on this property. I am trying to keep this strongly typed and correlated to my XSD, so if anyone knows how to change the name of the XML elements to their array name +1 answer for you.


Have you tried using the XmlArray and XmlArrayElement attributes?
http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlarrayattribute.aspx
http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlarrayitemattribute.aspx

[XmlArrayItem(ElementName="GenericItem", Type = typeof(Item))]
[XmlArrayItem(ElementName="BookItem", Type = typeof(BookItem))]
[XmlArray]
public Item []Items {...}


Try this :)

[XmlType(TypeName="MyItems")]
public class MyItems:List<Item>
{   }

The result xml is:

<MyItems xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Item>...</Item>
</MyItems>
0

精彩评论

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

关注公众号