将xml反序列化为c#对象

本文关键字:对象 反序列化 xml | 更新日期: 2023-09-27 18:21:23

我有以下xml(不能编辑):

<?xml version="1.0" encoding="UTF-8"?>
<ns0:prices xmlns:ns0="http://schemas.some.com/sadas/Output">
  <pricepoint>
    <esid>
      ENG.GPL.DAY_AHEAD.PROMPT.PH.M
    </esid>
    <observationdate>20120123</observationdate>
    <observationtime>0000</observationtime>
    <price>22.1250</price>
    <quote>Q</quote>
  </pricepoint>
  <pricepoint>
    <esid>
      ENG.NBP.DAY_AHEAD.PROMPT.PH.M
    </esid>
    <observationdate>20120123</observationdate>
    <observationtime>0000</observationtime>
    <price>53.8500</price>
    <quote>Q</quote>
  </pricepoint>
  <pricepoint>
    <esid>
      ENG.NCG.DAY_AHEAD.PROMPT.PH.M
    </esid>
    <observationdate>20120123</observationdate>
    <observationtime>0000</observationtime>
    <price>22.0750</price>
    <quote>Q</quote>
  </pricepoint>
  <pricepoint>
    <esid>
      ENG.TTF.DAY_AHEAD.PROMPT.PH.M
    </esid>
    <observationdate>20120123</observationdate>
    <observationtime>0000</observationtime>
    <price>21.9500</price>
    <quote>Q</quote>
  </pricepoint>
  <pricepoint>
    <esid>
      ENG.ZEEBRUGGE.DAY_AHEAD.PROMPT.PH.M
    </esid>
    <observationdate>20120123</observationdate>
    <observationtime>0000</observationtime>
    <price>53.6500</price>
    <quote>Q</quote>
  </pricepoint>
</ns0:prices>

以及我想将其映射到的以下对象:

[Serializable]
public class Prices 
{
    public List<Pricepoint> prices { get; set; }
}
[Serializable]
public class Pricepoint
{
    public string Esid { get; set; }
    public DateTime Observationdate { get; set; }
    public int Observationtime { get; set; }
    public double Price { get; set; }
    public string Quote { get; set; }
}

使用以下方法:

    public static object Deserialize(Type typeToDeserialize,byte[] bytes)
    {
        var mem = new MemoryStream(bytes);
        var ser = new XmlSerializer(typeToDeserialize);
        return ser.Deserialize(mem);
    }

Deserialize(typeof(Prices), byteArrayofXMLfile); 调用

然而,我确实得到了一个关于xml的名称空间行的错误:

There is an error in XML document (3, 2)

我不明白我在这里做错了什么?

将xml反序列化为c#对象

编辑:只是重读一下,发现你在问命名空间的问题。使用XmlRootAttributeNamespace属性,如下所示。


您需要告诉XmlSerializer哪些标记通过属性映射到哪些属性:

[Serializable]
[XmlRoot(Namespace = "http://www.ZomboCorp.com/", ElementName="prices")]
public class Prices 
{
    [XmlElement("pricepoint")]
    public List<Pricepoint> prices { get; set; }
}
[Serializable]
public class Pricepoint
{
    [XmlElement("esid")]
    public string Esid { get; set; }
    [XmlElement("observationdate")]
    public DateTime Observationdate { get; set; }
    [XmlElement("observationtime")]
    public int Observationtime { get; set; }
    [XmlElement("price")]
    public double Price { get; set; }
    [XmlElement("quote")]
    public string Quote { get; set; }
}

类似的东西?

XmlRootAttribute r = new XmlRootAttribute("prices");
r.Namespace = "http://schemas.some.com/sadas/Output";
var ser = new XmlSerializer(typeof(Prices),r);
return ser.Deserialize(mem);

PS:您还应该考虑标签名称的大小写

通过让visualstudio为我生成类来解决问题:

C:'>xsd latest.xml
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 4.0.30319.1]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file 'C:'latest.xsd'.
C:'>xsd latest.xsd /classes
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 4.0.30319.1]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file 'C:'latest.cs'.