This is a part of XML file I'm trying to de-serialize:
<entry>
...
<A:family type="user">
<A:variationCount>7</A:variationCount>
<A:part type="user">
<title>94 LPS</title>
<Voltage type="custom" typeOfParameter="Electrical Potential" units="V">120 V</Voltage>
<Unit_Length displayName="Unit Length" type="custom" typeOfParameter="Length" units="mm">540</Unit_Length>
<Unit_Height displayName="Unit Height" type="custom" typeOfParameter="Length" units="mm">222</Unit_Height>
<Total_Cooling_Capacity displayName="Total Cooling Capacity" type="custom" typeOfParameter="Power" units="W">1758 W</Total_Cooling_Capacity>
<Supply_Air_Width displayName="Supply Air Width" type="custom" typeOfParameter="Duct Size" units="mm">400 mm</Supply_Air_Width>
<Supply_Air_Height displayName="Supply Air Height" type="custom" typeOfParameter="Duct Size" units="mm">150 mm</Supply_Air_Height>
<Sensible_Cooling_Capacity displayName="Sensible Cooling Capacity" type="custom" typeOfParameter="Power" units="W">1348 W</Sensible_Cooling_Capacity>
<Return_Air_Width displayName="Return Air Width" type="custom" typeOfParameter="Duct Size" units="mm">475 mm</Return_Air_Width>
<Number_of_Poles displayName="Number of Poles" type="custom" typeOfParameter="Number of Poles">1</Number_of_Poles>
<Load_Classification displayName="Load Classification" type="custom" typeOfParameter="Load Classification">Cooling</Load_Classification>
<CFU_Material displayName="CFU Material" type="custom" typeOfParameter="Material"><By Category></CFU_Material>
<C2_Offset_1 displayName="C2 Offset 1" type="custom" typeOfParameter="Length" units="mm">108</C2_Offset_1>
<C1_Offset_1 displayName="C1 Offset 1" type="custom" typeOfParameter="Length" units="mm">108</C1_Offset_1>
<Apparent_Load displayName="Apparent Load" type="custom" typeOfParameter="Apparent Power" units="VA">50 VA</Apparent_Load>
</A:part>
<A:part type="user">
...
</A:part>
...
</A:family>
</entry>
These are classes I'm using to de-serialize it:
[XmlType(AnonymousType = true)]
[XmlRoot("entry", Namespace="http://www.w3.org/2005/Atom")]
public class PartAtom
{
...
[XmlElement("family", Namespace="urn:schemas-autodesk-com:partatom")]
public Family Family { get; set; }
}
public class Family
{
[XmlAttribute("type")]
public string Type { get; set; }
[XmlElement("variationCount")]
public int VariationCount { get; set; }
[XmlElement("part")]
public FamilyType[] Parts { get; set; }
}
[XmlRoot(Namespace = "http://www.w3.org/2005/Atom")]
public class FamilyType
{
[XmlAttribute("type")]
public string Type { get; set; }
[XmlElement("title")]
public string Title { get; set; }
[XmlArray]
public Parameter[] Parameters { get; set; }
}
[XmlRoot(Namespace = "http://www.w3.org/2005/Atom")]
public struct Parameter
{
[XmlAttribute("displayName")]
public string Name { get; set; }
[XmlAttribute("type")]
public string Type { get; set; }
[XmlAttribute("typeOfParameter")]
public string DataType { get; set; }
[XmlText]
public string Value { get; set; }
[XmlAttribute("units")]
public string Units { get; set; }
}
I want the elements such as Voltage, Units_Length, Unit_Height, ... Apparent_Load etc to be de-serialized as instances of Parameters class. How can I accomplish something like that? Is it even possible?
UPDATE:
The parameters (XML elements below the title) are virtually infinite, so I can't contemplate all of them, so all the algorithms where I have to specify XmlElement names manually are unusable.
If you don't want to implement
IXmlSerializable
on your class, one option would be to add a proxy property ofXElement []
elements marked with[XmlAnyElement]
and serialize the parameters from and to this list, fixing the names as required.Assuming your XML has namespace declarations on the root element like the following that were omitted from the question:
Then the following should work:
Note that the serializer automatically deserializes the
Title
andType
properties rather than passing then to theAnyElement
array. Then, inParameter
, I changedName
toDisplayName
and added anElementName
property to hold the element name:Using the extension and helper methods:
Still, easier than a custom
IXmlSerializable
.One solution might be multiple
XmlElementAttribute
with different names specified.Something like this:One (ugly) way of doing it, could be to modify the XML before you de-serialize it. Just string-replace all of the "Voltage", "Unit_height" etc with "YourParameterClassName". Then they should all nicely deserialize, and I'm guessing that your "typeOfParameter" will still provide you with the same information that originally was present in the nodes before the replacement.