C # xml serialization extra root node

I have a collection that I want to serialize in an XML document. Grade:

public class Contacts{ public List<PendingContactDTO> contacts { get; set; } } 

My main problem is that now my xml looks like

 <Contacts> <contacts> <..... all contacts> </contacts> </Contacts> 

The fact is that I want to look like this:

  <contacts> <..... all contacts> </contacts> 

Is there any way?

+4
source share
2 answers
 [XmlRoot("contacts")] public class Contacts{ [XmlElement("contact")] public List<PendingContactDTO> contacts { get; set; } } 

should provide you with:

 <contacts> <contact...>...</contact> ... <contact...>...</contact> </contacts> 

( XmlRootAttribute renames Contacts to Contacts ; XmlElementAttribute tells him to remove the extra layer for the node collection, calling each contact )

+6
source

load xml in XmlDocument

 xmlDoc.LoadXml(StrXML); xmlDoc.SelectSingleNode("/Contacts/contacts") 

I hope this helps you

0
source

All Articles