Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将xml反序列化为c对象_C#_Xml_Serialization - Fatal编程技术网

C# 将xml反序列化为c对象

C# 将xml反序列化为c对象,c#,xml,serialization,C#,Xml,Serialization,我有以下无法编辑的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>201

我有以下无法编辑的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>
它由反序列化的定价类型、tearrayofxmlfile调用

但是,我确实收到了一个关于xml的名称空间行的错误:

There is an error in XML document (3, 2)
我想不出我做错了什么

编辑: 只要重新阅读,就会发现您正在询问名称空间。使用XmlRootAttribute的Namespace属性,如下所示

您需要告诉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的Namespace属性,如下所示

您需要告诉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:你也应该考虑标签名称

这样的事情吗?< /P>
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:还应该考虑标签名称的外壳< /p> ,通过让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'.

通过让visual studio为我生成类解决了此问题:

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'.

您应该使用数据协定序列化程序序列化数据协定。不必担心数据协定,它们不应该在那里,将进行编辑:您应该使用数据协定序列化程序序列化数据协定。不必担心数据协定,它们不应该在那里,将进行编辑: