我有这个 XML 结构(来自一个更大的文件——只有这部分会出现问题)
<Table>
<Row id="1">
<Heading>sgjsfgjsgfh443q572q356</Heading>
<Items>
<Item car="motor1" id="1">
<BodyText color="red">130*</BodyText>
<Subscript>3</Subscript>
</Item>
</Items>
</Row>
</Table>
并尝试使用XmlSerializer
此模型将其反序列化(对于与 XML 匹配的部分也是 excert):
[XmlRoot("Table")]
public partial class Table
{
[XmlElement("Row")]
public Row[] Row { get; set; }
}
[XmlRoot("Row")]
public partial class Row
{
[XmlElement("Heading")]
public string Heading { get; set; }
[XmlElement("Items")]
public Item[] Items { get; set; }
[XmlElement("BodyText")]
public BodyText BodyText { get; set; }
[XmlAttribute("id")]
public string id { get; set; }
}
[XmlRoot("Items")]
public partial class Items
{
[XmlElement("Item")]
public Item[] Item { get; set; }
}
[XmlRoot("Item")]
public partial class Item
{
[XmlElement("BodyText")]
public BodyText BodyText { get; set; }
[XmlElement("PhoneNumber")]
public PhoneNr[] PhoneNr { get; set; }
[XmlElement("Subscript")]
public Subscript[] Subscript { get; set; }
[XmlAttribute("car")]
public string car { get; set; }
[XmlAttribute("id")]
public string id { get; set; }
}
[XmlRoot("BodyText")]
public partial class BodyText
{
[XmlAttribute("color")]
public string color { get; set; }
[XmlAttribute("fonttype")]
public string fonttype { get; set; }
[XmlAttribute("fontsize")]
public string fontsize { get; set; }
[XmlAttribute("fontweight")]
public string fontweight { get; set; }
[XmlText]
public string Value { get; set; }
}
[XmlRoot("Subscript")]
public partial class Subscript
{
[XmlAttribute("for")]
public string @for { get; set; }
[XmlText]
public string Value { get; set; }
}
[XmlRoot("PhoneNr")]
public partial class PhoneNr
{
[XmlElement("Display")]
public string Display { get; set; }
[XmlElement("Number")]
public string Number { get; set; }
[XmlAttribute("id")]
public string id { get; set; }
}
类的结果对象Table
包含一个Row
元素,这很好,但是:里面的元素Row
都是空的。似乎序列化程序无法与Items
他们的类匹配。
我能做什么,所以Items
andItem
被正确反序列化为对象?