我有这种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匹配的部分也是例外):
[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
中的元素都是null。似乎序列化程序无法将Items
与其类匹配。
我该怎么办,Items
和Item
是否正确反序列化为对象?
答案 0 :(得分:1)
你可以为下标,PhoneNr和BodyText添加你的XmlRoot吗?这样我可以做一些更好的测试。
我还建议:创建对象图并对其进行序列化。通过这种方式,您可以看到它是如何序列化的。
编辑:这是我从我创建的对象反序列化的XML。如您所见,项目嵌套不正常,所以我猜这也是序列化时的问题。我会随时通知你
<?xml version="1.0" encoding="utf-8"?>
<Table xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Row>
<Heading>myHeading</Heading>
<Items car="myCar" id="myId">
<BodyText color="Red">135</BodyText>
<Subscript>3</Subscript>
</Items>
<Items car="myCar" id="myId">
<BodyText color="Red">135</BodyText>
<Subscript>3</Subscript>
</Items>
</Row>
</Table>
答案 1 :(得分:1)
我发现了我认为的错误。 在您的类Row中,属性项应该是Type Items而不是Item []。
当我进行此更改时,我可以进行正确的序列化和反序列化。
这是Serialize和Deserialize代码+我使用的类。
private void button1_Click(object sender, EventArgs e)
{
//Serialize
//var x = File.ReadAllText(@"C:\TableInfo.xml");
//var stringReader = new StringReader(x);
//var deserializer = new XmlSerializer(typeof(Table));
//var myTable = (Table)deserializer.Deserialize(stringReader);
//Deserialize
var myTable2 = new Table();
myTable2.Row = new Row[1];
myTable2.Row[0] = new Row();
myTable2.Row[0].id = "myId";
myTable2.Row[0].Heading = "myHeading";
myTable2.Row[0].Items = new Items();
myTable2.Row[0].Items.Item = new Item[1];
myTable2.Row[0].Items.Item[0] = new Item();
myTable2.Row[0].Items.Item[0].BodyText = new BodyText() { color = "Red" };
myTable2.Row[0].Items.Item[0].BodyText.Value = "135";
myTable2.Row[0].Items.Item[0].car = "myCar";
myTable2.Row[0].Items.Item[0].id = "myId";
myTable2.Row[0].Items.Item[0].Subscript = new Subscript[1];
myTable2.Row[0].Items.Item[0].Subscript[0] = new Subscript();
myTable2.Row[0].Items.Item[0].Subscript[0].Value = "3";
XmlSerializer serializer = new XmlSerializer(typeof(Table));
TextWriter textWriter = new StreamWriter(@"C:\TableInfo.xml");
serializer.Serialize(textWriter, myTable2);
textWriter.Close();
}
[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 Items 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; }
}