如何在不使用复数父元素的情况下从xml反序列化列表?

wei*_*in8 3 c# xml-serialization

我想将HTML表反序列化为一个对象。但是,用下面的代码,它希望看到<Rows>作为母公司<tr>的和<Cells>作为母公司<td>的。我想保持这个类的结构。我是否缺少任何属性声明?

<table>
  <tr>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td></td>
    <td></td>
  </tr>
</table>

public class Table
{
    [XmlArrayItem("tr")]
    public List<Row> Rows { get; set; }
}

public class Row
{
    [XmlArrayItem("td")]
    public List<Cell> Cells { get; set; }
}

public class Cell
{
    public object Value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

rsb*_*rro 5

尝试如下所示设置属性。请注意,您必须改变的类型ValueCell从类objectstring

[XmlRoot("table")]
public class Table
{
    [XmlElement(typeof(Row), ElementName = "tr")]
    public List<Row> Rows { get; set; }
}

public class Row
{
    [XmlElement(typeof(Cell), ElementName = "td")]
    public List<Cell> Cells { get; set; }
}

public class Cell
{
    [XmlText(typeof(string))]
    public string Value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)