I'm trying to create an XML output file to import into another program. The example XML file I was given looks like this:
<SalesOrder>
<OrderHeader>
<BillTo>
<EntityID>1234</EntityID>
</BillTo>
</OrderHeader>
<LineItemList>
<OrderLineComment>
<LineItemID>1</LineItemID>
</OrderLineComment>
<LineItem>
...
</LineItem>
<LineItem>
...
</LineItem>
<LineItem>
...
</LineItem>
...
</LineItemList>
</SalesOrder>
I have a C# project that is able to output this type of file using an XmlSerializer with the exception of this part:
<LineItemList>
<OrderLineComment>
<LineItemID>1</LineItemID>
</OrderLineComment>
The LineItemList section is simply a list of LineItems, but at the beginning of the LineItemList there is tacked this different element OrderLineComment.
If I represent this as an array of LineItems, then it looks the same except it's missing the OrderLineComment. If I represent this as a new object LineItemList containing an OrderLineComment and an array of LineItems, I get this:
<LineItemList>
<OrderLineComment>
<LineItemID>1</LineItemID>
</OrderLineComment>
<LineItems>
<LineItem>
...
</LineItem>
...
</LineItems>
Which has what I want, except it wraps all the LineItems with the <LineItems>
tag, which isn't what I want either.
So what I'm wondering is:
- Is there a way to do this via XmlSerializer? If so, how?
- If there isn't, and I have to rewrite the code to use something other than XmlSerializer, what would be the best way to do this and why?
Thanks in advance.