【发布时间】:2015-09-03 19:47:41
【问题描述】:
这是我试图反序列化的 XML 文件的一部分:
<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>
这些是我用来反序列化它的类:
[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; }
}
我希望将电压、Units_Length、Unit_Height、... Apparent_Load 等元素反序列化为参数类的实例。 我怎样才能完成这样的事情?有没有可能?
更新:
参数(标题下方的 XML 元素)实际上是无限的,所以我无法考虑所有这些参数,因此我必须手动指定 XmlElement 名称的所有算法都无法使用。
【问题讨论】:
-
只使用这个表单的属性,我不这么认为。
Parameter需要一个名称,它不能反序列化具有多个名称的元素。每个参数类型都需要一个类。如果必须采用这种形式,请考虑实现IXmlSerializable。 -
@Charles Mager > 每个参数类型都需要一个类" 是的,这将是主要问题,但由于这些参数是由用户创建的,我无法考虑所有这些,所以这个策略不行。另一方面,实现
IXmlSerializable真的很烦人,因为默认情况下,如果我离开 XmlAttribute/XmlText 属性,它不会填充“已知”元素,事件。你知道如何避免这种行为吗?我的意思是Name,DataType,Units等不会自动填充,大概我也必须从ReadXml方法初始化它们:( -
在
ReadXml()中,您可以加载到XElement并使用嵌套的XmlSerializer反序列化,如下所示:stackoverflow.com/questions/28769495/… -
你的根元素
<entry>必须有几个这样的命名空间,对吧?<entry xmlns="http://www.w3.org/2005/Atom" xmlns:A="urn:schemas-autodesk-com:partatom">
标签: c# xml serialization deserialization xml-deserialization