我有以下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)
我无法弄清楚我在这里做错了什么?
答案 0 :(得分:2)
修改强>:
只是重新阅读,看到你问的是命名空间。使用Namespace
的{{1}}属性,如下所示。
您需要告诉XmlSerializer哪些标签通过属性映射到哪些属性:
XmlRootAttribute
答案 1 :(得分:1)
这样的东西?
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:您还应该考虑标记名称的大小写
答案 2 :(得分:1)
通过让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'.