Reputation: 2783
i have the following xml (which i can not edit):
<?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>
and the following object i want to map it to:
[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; }
}
Using the following method:
public static object Deserialize(Type typeToDeserialize,byte[] bytes)
{
var mem = new MemoryStream(bytes);
var ser = new XmlSerializer(typeToDeserialize);
return ser.Deserialize(mem);
}
which is invoed by Deserialize(typeof(Prices), byteArrayofXMLfile);
I do however get an error regarding the namespace line of the xml:
There is an error in XML document (3, 2)
I cant figure out what i am doing wrong here?
Upvotes: 1
Views: 6849
Reputation: 2783
resolved the problem by letting visual studio generating the classes for me:
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'.
Upvotes: 1
Reputation: 152624
EDIT:
Just re-read and saw you were asking about the namespace. Use the Namespace
property of XmlRootAttribute
as shown below.
You'll need to tell the XmlSerializer what tags map to what properties through attributes:
[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; }
}
Upvotes: 2
Reputation: 116178
Something like this?
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: you should also consider casing of tag names
Upvotes: 1