我有这样的XML结构(excert从更大的文件 - 只有这部分对问题)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
反序列化是这种模式(也excert匹配的零件XML):
[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
类产生的对象包含单个元件,这是很好的,但:内部元素均为空。看来序列化程序不能匹配Items
到他们的类。
我该怎么做,所以Items
和Item
被正确反序列化成对象?
我加了缺失的根。我如何创建一个对象图?我从来没有听说过这个词。 – 2013-02-26 16:40:18
谢谢...通过对象图我只是指内存中的对象(var myTable = new Table()等等,然后序列化它) – deblendewim 2013-02-26 16:44:06