2013-02-26 130 views
1

我有这样的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到他们的类。

我该怎么做,所以ItemsItem被正确反序列化成对象?

回答

1

我发现了我认为的错误。 在你的类行中,属性项应该是类型项而不是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; } 
} 
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> 
+0

我加了缺失的根。我如何创建一个对象图?我从来没有听说过这个词。 – 2013-02-26 16:40:18

+0

谢谢...通过对象图我只是指内存中的对象(var myTable = new Table()等等,然后序列化它) – deblendewim 2013-02-26 16:44:06