Undesired XML Serialization Property Name

Possible duplicate:
Removing Wrapper Elements from an XML Serialized Array

This is hard to explain, so I include an example of my problem. I have a parent class containing a list of child classes. When I serialize the parent, I get my child classes, but they are under the element with the name of the public property. An extra level is not what I need. I tried adding the XmlIgnore attribute to the property name, but it suppressed the entire property name and the collection of the invoices it contains.

Parent class:

[XmlRoot("header")] public class Lynx : INotifyPropertyChanged { #region /*-- Class Fields --*/ private List<InvoiceItem> _invoice = new List<InvoiceItem>(); #endregion [XmlArray("invoice")] [XmlArrayItem("invoice", typeof(InvoiceItem))] public List<InvoiceItem> invoice { get { return _invoice; } set { if (value != _invoice) { _invoice = value; OnPropertyChanged("invoice"); } } } 

Child class:

 [XmlType(TypeName = "invoice")] public class InvoiceItem : INotifyPropertyChanged { ... properties and methods of the class } 

This is what it creates:

 <header> <headerid>790aa61a-ad1b-49b9-bfb9-01fe3ca55eca</headerid> <invoice> <-- this line is not needed <invoice> <company>BRU111</company> <format>myformat</format> ... 

This is what I need to build:

 <header> <headerid>790aa61a-ad1b-49b9-bfb9-01fe3ca55eca</headerid> <invoice> <company>BRU111</company> <format>myformat</format> ... 
+4
source share
1 answer

The reason an extra item is created is because your property is a list of items

If you want to get an exceptional value, use

InvoiceItem public invoice
{

instead

open list <InvoiceItem> invoice
{

Collections always place a container, so each element is placed under the node container.

-1
source

Source: https://habr.com/ru/post/1385207/


All Articles