I am having trouble finding why the following XML won't deserialize.
Message:
开发者_开发问答There is an error in XML document (23, 26).
Inner exception:
Input string was not in a correct format.
Calling Code (works without issue on everything except the XML below):
Public Shared Function DeserializeObject(Of T)(ByVal serializedXml As String) As T
Dim serializer As New XmlSerializer(GetType(T))
Using strReader As New IO.StringReader(serializedXml)
Return DirectCast(serializer.Deserialize(strReader), T)
End Using
End Function
XML (looks ok - issue is after the first open square bracket of DiscountCode):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<MyResponse>
<Shoppinglist>
<ShoppinglistName>ShoppingListName</ShoppinglistName>
<Currency>GBP</Currency>
<PriceType>Retail</PriceType>
<Orders>
<Order>
<OrderLineId>Test</OrderLineId>
<Completed>false</Completed>
<Description>Other Item</Description>
<Quantity>1</Quantity>
<Parts>
<Part>
<PartId>43543543435</PartId>
<Description>Cylinder Assy - Master</Description>
<PriceInclTax>92.16</PriceInclTax>
<PriceExclTax>76.8</PriceExclTax>
<Quantity>1</Quantity>
<TaxRate>20.0</TaxRate>
<NetDiscountCode>ZZ</NetDiscountCode>
<SurchargeQuantity>1.0</SurchargeQuantity>
-------------> <DiscountRate>0.0</DiscountRate>
</Part>
</Parts>
</Order>
</Orders>
</Shoppinglist>
<ReturnCode>1</ReturnCode>
<StatusMessage>Open tasks exist</StatusMessage>
</MyResponse>
Can anyone see what is wrong?
Stack trace (for outer exception):
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(TextReader textReader)
Stack trace for inner exception:
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Xml.XmlConvert.ToInt32(String s)
Which is strange, as it looks like it is parsing to Int, when it should be parsing to decimal...
property for class T:
Protected _DiscountRate As Decimal
Public Property DiscountRate() As Decimal
Get
Return _DiscountRate
End Get
Set(ByVal Value As Decimal)
_DiscountRate = Value
End Set
End Property
I added the following, but it didn't make any difference:
<System.Xml.Serialization.XmlElement(Type:=GetType(Decimal))> _
I doubt this is to do with strange characters as I can't see any. Also changing the type to Double does not work around the problem. Any other ideas?
A VERY important note about XML error line and column numbers: The XML declaration is not counted as a line, so position (0,0) in your file is actually the second line, not the line with the declaration
EDIT: sorry, my point is that if the exception says there's something wrong at (23, 26), it's actually at (24,26).
As a work around you can change type of the DiscountRate from decimal
to double
and see whether it works, it takes less memory and should be enough for such business entity like discount rate.
It turns out the problem was with a property, however, I was so busy looking at what the error message was saying, I missed it. The error message pointed to the wrong property!
精彩评论