1

我有这个 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他们的类匹配。

我能做什么,所以ItemsandItem被正确反序列化为对象?

4

2 回答 2

1

您能否为 Subscript、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>
于 2013-02-26T16:34:23.183 回答
1

我发现了我认为的错误。在您的类 Row 中,Property Items 应该是 Type Items 而不是 Item[]。

当我进行此更改时,我可以进行适当的序列化和反序列化。

这是序列化和反序列化代码+我使用的类。

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; }
}
于 2013-02-26T17:31:02.600 回答