Demining xml on an object raises an error
<?xml version="1.0" encoding="UTF-8"?>
<Order xmlns="urn:schemas-alibaba-com:billing-data" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Currency>USD</Currency>
<Description>description</Description>
</Order>
I have the above xml string that I am trying to deserialize to an object. This is an auto-generated class.
[XmlTypeAttribute(AnonymousType = true,
Namespace = "urn:schemas-alibaba-com:billing-data")]
[XmlRootAttribute(ElementName="Order",
Namespace = "urn:schemas-alibaba-com:billing-data",
IsNullable = false)]
public partial class Order
{
private string currencyField;
private object descriptionField;
}
I am getting an exception:
Exception:
{"There is an error in XML document (1, 2)."}
Inner exception :
{"<Order xmlns='urn:schemas-alibaba-com:billing-data'> was not expected."}
What am I missing here? Below is the deserialization code: line 3 throws an exception.
var xmlReader = new StringReader(xml_data);
var serializer = new XmlSerializer(typeof(Order));
var instance = (Order)serializer.Deserialize(xmlReader);
+3
source to share
1 answer
I am testing your xml content, this is ok.
Here is my code:
[TestMethod]
public void Xml_ShouldBeDeserialized()
{
var serializer = new XmlSerializer(typeof (Order));
using (var stream = File.OpenRead(@"D:\test.xml"))
{
var obj = serializer.Deserialize(stream);
var order = obj as Order;
Assert.IsNotNull(order);
}
}
[XmlTypeAttribute(AnonymousType = true,
Namespace = "urn:schemas-alibaba-com:billing-data")]
[XmlRoot(ElementName = "Order",
Namespace = "urn:schemas-alibaba-com:billing-data",
IsNullable = false)]
public partial class Order
{
private string currencyField;
private object descriptionField;
public string Currency { get; set; }
public string Description { get; set; }
}
I think you can skip the Currency and Description fields and they should be available,
+2
source to share