Domanda

I am retrieving and successfully deserializing an xml string from a database table column for the known element names (these do not change) but there are also some nested XML Elements called "Other Attributes" which are not always going to be known. I'm having some trouble deserialising these unknown element names to a generic list so I can display them in html once deserialised.

The XML is as follows:

<Detail>
<DetailAttributes>
<Name>Name_123</Name>
<Type>Type_123</Type>
</DetailAttributes>
<OtherAttributes>
<SummaryKey AttributeName="SummaryKey">SummaryKey_123</SummaryKey>
<Account AttributeName="Account">Account_123</Account>
</OtherAttributes>
</Detail>

I have no problem deserialising the 'Name' and 'Type' elements and I can deserialise the 'SummaryKey' and 'Account' elements but only if I explicitly specify their element names - which is not the desired approach because the 'OtherAttributes' are subject to change.

My classes are as follows:

[XmlRoot("Detail")]
public class objectDetailsList
{
    [XmlElement("DetailAttributes"), Type = typeof(DetailAttribute))]
    public DetailAttribute[] detailAttributes { get; set; }

    [XmlElement("OtherAttributes")]
    public List<OtherAttribute> otherAttributes { get; set; }

    public objectDetailsList()
    {
    }
}
[Serializable]
public class Detail Attribute
{
    [XmlElement("Type")]
    public string Type { get;set; }

    [XmlElement("Name")]
    public string Name { get;set; }

    public DetailAttribute()
    {
    }
}

[Serializable]
public class OtherAttribute
{
    //The following will deserialise ok

    //[XmlElement("SummaryKey")]
    //public string sumKey { get; set; }

    //[XmlElement("Account")]
    //public string acc { get; set; }

    //What I want to do below is create a list of all 'other attributes' without known names

    [XmlArray("OtherAttributes")]
    public List<Element> element { get; set; }
}

[XmlRoot("OtherAttributes")]
public class Element
{
    [XmlAttribute("AttributeName")]
    public string aName { get; set; }

    [XmlText]
    public string aValue { get; set; }
}

When I try to retrieve the deserialised list of OtherAttribute elements the count is zero so it's not able to access the elements nested within "Other Attributes".

Can anybody help me with this please?

È stato utile?

Soluzione

With concrete classes and dynamic data like this, you won't be able to lean on the standard XmlSerializer to serialize / deserialize for you - as it reflects on your classes, and the properties you want to populate simply don't exist. You could provide a class with all possible properties if your set of 'OtherAttributes' is known and finite, and not subject to future change, but that would give you an ugly bloated class (and I think you've already decided this is not the solution).

Practical options therefore:

  • Do it manually. Use the XmlDocument class, load your data with .Load(), and iterate the nodes using .SelectNodes() using an XPath query (something like "/Detail/OtherAttributes/*"). You will have to write the lot yourself, but this gives you complete control over the serialization / deserialization. You won't have to cover your code in (arguably superfluous!) attributes either.

  • Use Json.NET (http://james.newtonking.com/json), it allows for far greater control over serialization and deserialization. It's fast, has good docs and is overall pretty nifty really.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top