反序列化XML字符串不起作用

时间:2013-07-03 05:41:31

标签: c# xml-serialization xml-deserialization

我有这个XML字符串

<Root>
  <Row>
    <ITEMNO>1</ITEMNO>
    <PARTSOURCE>Buy</PARTSOURCE>
    <QTY>2</QTY>
    <CUSTPARTNO>469</CUSTPARTNO>
    <DESCRIPT>35W 1/4W 5%</DESCRIPT>
  </Row>
  <Row>
    <ITEMNO>3</ITEMNO>
    <PARTSOURCE>Buy</PARTSOURCE>
    <QTY>5</QTY>
    <CUSTPARTNO>116</CUSTPARTNO>
    <DESCRIPT>1.74K 1/8W 1% Film</DESCRIPT>
  </Row>
  <Row>
    <ITEMNO>2</ITEMNO>
    <PARTSOURCE>MAKE</PARTSOURCE>
    <QTY>5</QTY>
    <CUSTPARTNO>651321</CUSTPARTNO>
    <DESCRIPT>Make Part</DESCRIPT>
  </Row>
</Root>

我正在尝试将其反序列化为此Root

public class Root
{
    public BOMItems Row { get; set; }
}
public class BOMItems
{
    public string ITEMNO { get; set; }
    public string USED { get; set; }
    public string PARTSOURCE { get; set; }
    public string QTY { get; set; }
    public string CUSTPARTNO { get; set; }
    public string CREV { get; set; }
    public string DESCRIPT{ get; set; }
}

使用此方法:

public Root TransformXMLToClass(string pXml)
{
    var serializer = new XmlSerializer(typeof(Root));
    XmlReader reader = XmlReader.Create(new StringReader(pXml));

    return (Root)serializer.Deserialize(reader);
}

这仅适用于第一个节点。

我尝试过像这样更改Root类:

    public List<BOMItems> Row{get;set;}

它不适用于任何节点

如何让它反序列化所有节点?

2 个答案:

答案 0 :(得分:2)

在你的情况下,它很简单。您只需告诉序列化程序如何处理元素集合。

您需要将属性作为集合或数组,如下所示:

public class Root
{
    [XmlElement("Row")]
    public BOMItems[] Row { get; set; }
}

“[XmlElement]”将告诉它如何正确反序列化。添加XmlElementAttribute会告诉序列化程序将其视为“平坦”序列。

<!-- Row Elements Flattened -->
<Root>
  <Row>
    <ITEMNO>1</ITEMNO>
    <PARTSOURCE>Buy</PARTSOURCE>
    <QTY>2</QTY>
    <CUSTPARTNO>469</CUSTPARTNO>
    <DESCRIPT>35W 1/4W 5%</DESCRIPT>
  </Row>
  <Row>
    <ITEMNO>3</ITEMNO>
    <PARTSOURCE>Buy</PARTSOURCE>
    <QTY>5</QTY>
    <CUSTPARTNO>116</CUSTPARTNO>
    <DESCRIPT>1.74K 1/8W 1% Film</DESCRIPT>
  </Row>
</Root>

    <!-- Versus Row Elements Unflattened -->
<Root>
  <Row>
    <BOMItems>
      <ITEMNO>1</ITEMNO>
      <PARTSOURCE>Buy</PARTSOURCE>
      <QTY>2</QTY>
      <CUSTPARTNO>469</CUSTPARTNO>
      <DESCRIPT>35W 1/4W 5%</DESCRIPT>
    </BOMItems>
    <BOMItems>
      <ITEMNO>3</ITEMNO>
      <PARTSOURCE>Buy</PARTSOURCE>
      <QTY>5</QTY>
      <CUSTPARTNO>116</CUSTPARTNO>
      <DESCRIPT>1.74K 1/8W 1% Film</DESCRIPT>
    </BOMItems>
  </Row>
</Root>

答案 1 :(得分:1)

您的BOM类应如下所示

[XmlElement("ITEMNO")]
public string ITEMNO { get; set; }

以便通知序列化程序您已阅读ITEMNO并将其保存在ITEMNO中

编辑: 必须为BOM类的每个属性执行此操作。

答案:(附上清单)

public class Root
{
    [XmlElement("Row")]
    public List<BOMItems> Row { get; set; }
}
public class BOMItems
{
    [XmlElement("ITEMNO")]
    public string ITEMNO { get; set; }
    [XmlElement("USED")]
    public string USED { get; set; }
    [XmlElement("PARTSOURCE")]
    public string PARTSOURCE { get; set; }
    [XmlElement("QTY")]
    public string QTY { get; set; }
    [XmlElement("CUSTPARTNO")]
    public string CUSTPARTNO { get; set; }
    [XmlElement("CREV")]
    public string CREV { get; set; }
    [XmlElement("DESCRIPT")]
    public string DESCRIPT { get; set; }
}
class Program
{
    static void Main(string[] args)
    {

        var serializer = new XmlSerializer(typeof(Root));
        var reader = XmlReader.Create(new StringReader(File.ReadAllText("c:\\tet.xml")));

        var serializedOutput = (Root)serializer.Deserialize(reader);
        Console.ReadKey();
    }