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?
source share