XmlSerialization Collections

I want to serialize the following Xml structure:

<XmlRootElement> <Company name="Acme Widgets LLC"> <DbApplication name="ApplicationA" vendor="oracle"> <ConnSpec environment="DEV" server="DBOraDev1201" database="AppA" userId="MyUser" password="5613456#6%^%$%$#" /> <ConnSpec environment="QA" server="DBOraQA1205" database="AppA" userId="MyUser" password="5613456#6%^%$%$#" /> </DbApplication> <DbApplication name="AnotherApp" "vendor="SQLServer"> <ConnSpec environment="DEV" server="DBMsSqlDev1201" catalog="AnoptherApp" userId="MyUser" password="5613456#6%^%$%$#" /> <ConnSpec environment="QA" server="DBMsSqlQA1565" catalog="AnotherApp" userId="MyUser" password="5613456#6%^%$%$#" /> </DbApplication> </Company> <Company name = "ExpertSoftware Inc" .... ... </Company> </XmlRootElement> 

but I found in the link text

Quote from the link above: ...

  • Q: Why are not all properties serialized classes?
  • A: XmlSerializer only serializes elements in the collection when it detects either an IEnumerable or an ICollection interface. This design behavior. The only work around is to override the collection order into two classes, one of which exposes properties including one of the pure collection types.

...

after discovering that you cannot serialize or deserialize a collection that has other Xml attributes ... The suggested workaround is to separate an element that has a collection from those that have other attributes ... t. e. You must change the structure so that it looks like this:

  <XmlRootElement> <Companys> <Company name="Acme Widgets LLC"> <DbApplications> <DbApplication name="ApplicationA" vendor="oracle"> <ConnSpecs> <ConnSpec environment="DEV" server="DBOraDev1201" ... /> <ConnSpec environment="QA" server="DBOraQA1205" database="AppA" ... /> </ConnSpecs> </DbApplication> <DbApplication name="AnotherApp" "vendor="SQLServer"> <ConnSpecs> <ConnSpec environment="DEV" ... /> <ConnSpec environment="QA" ... /> </ConnSpecs> </DbApplication> </DbApplications> </Company> <Company name = "ExpertSoftware Inc" .... ... </Company> </Companys> </XmlRootElement> 

Does anyone know why this is so? or if there is some other way to do this?

+3
source share
2 answers

If you are using an XmlSerializer , you need to specify how to serialize your collections:

 public class XmlRootElement { [XmlElement(ElementName="Company")] public Company[] Company { get; set; } // Other properties .... } public class Company { [XmlAttribute(AttributeName="name")] public string Name { get; set; } [XmlElement(ElementName = "DbApplication")] public DbApplication[] DbApplication { get; set; } // Other properties .... } public class DbApplication { [XmlElement(ElementName = "ConnSpec")] public ConnSpec[] ConnSpec { get; set; } // Other properties .... } public class ConnSpec { // Other properties .... } 

And then:

 using (Stream stream = new FileStream("test.xml", FileMode.Open, FileAccess.Read, FileShare.Read)) { XmlSerializer serializer = new XmlSerializer(typeof(XmlRootElement)); XmlRootElement root = (XmlRootElement)serializer.Deserialize(stream); } 

Hope this helps.

+2
source

An alternative to adding XMl serialization attributes to all your classes is to implement the IXmlSerializable interface in the root element container class. The ReadXml and WriteXml methods of this interface will be used by the XmlSerializer, which allows you to have finer-grained control over the serialization / deserialization process.

+1
source

All Articles