5

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, 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.

1
  • You should be able to use overrides. Show the code you used that generated an error. Commented Feb 16, 2010 at 2:00

2 Answers 2

19

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 {...}
Sign up to request clarification or add additional context in comments.

1 Comment

Problem is that the XML schema isn't controlled by me. Therefore I'm trying to avoid editing code in the class if possible.
8

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.