使用强类型的XSD错误deserialising XML文档 [英] Error deserialising XML document with strongly typed XSD
问题描述
我遇到一个很烦人的问题,而试图用deserialise XmlSerializer.Deserialize()方法的特定的XML文档。
I am running into quite an annoying issue while trying to deserialise a specific XML document using XmlSerializer.Deserialize() method.
基本上,我有一个强类型的XSD DOUBLE类型的元素。当试图deserialise元素有特定的XML文档,我得到了一贯的System.FormatException:输入字符串的不正确的格式。异常是因为特定文件中,元素不具有价值。
Basically, I have a strongly typed XSD with an element of type double. When trying to deserialise the element for a specific XML document, I get the usual "System.FormatException: Input string was not in a correct format." exception because in that specific document, the element does not have a value.
下面是为您的书呆子在那里一些代码。
Here is some code for you nerds out there.
的示例XML文档:
<TrackInfo>
<Name>Barcelona</Name>
<Length>4591</Length>
<AverageSpeed />
</TrackInfo>
XSD:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="TrackInfo">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="xs:string" />
<xs:element name="Length" type="xs:double" default="0.0" />
<xs:element name="AverageSpeed" type="xs:double" default="0.0" />
</xs:sequence>
</xs:complexType>
</xs:element>
TrackInfo类:
[Serializable]
public class TrackInfo
{
private string name = string.Empty;
private double length = 0.0;
private double averageSpeed = 0.0;
[XmlElement]
public string Name
{ ... }
[XmlElement]
public double Length
{ ... }
[XmlElement]
public double AverageSpeed
{ ... }
}
样品deserialisation代码:
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load("TrackInfo.xml");
// Deserialise XML string into TrackInfo object
byte[] buffer = ASCIIEncoding.UTF8.GetBytes(xmlDocument.InnerXml);
MemoryStream stream = new MemoryStream(buffer);
System.Xml.XmlReader reader = new System.Xml.XmlTextReader(stream);
XmlSerializer xSerializer = new System.Xml.Serialization.XmlSerializer(typeof(TrackInfo));
TrackInfo trackInfo = (TrackInfo)xSerializer.Deserialize(reader);
我知道deserialisation例外来自于一个事实,一个空字符串不能转换为double。我也知道这一点,因为有效,空字符串是一个完全可以接受的值的默认值没有分配给AverageSpeed。
I know that the deserialisation exception comes from the fact that an empty string cannot be converted to a double. I also know that the default value is not assigned to AverageSpeed because, effectively, an empty string is a perfectly acceptable value.
有一种简单的方式来默认双值0.0(或任何其他类型)deserialising时,如果一个空字符串值在XML文档中发现了什么?
理想情况下,我想,以避免实施ISerializable的,因为我不觉得自己是这一天的休息花费入地狱燃烧坑(即实施了ISerializable约一百班)。
Is there an easy way to default double values to 0.0 (or any other type) when deserialising if an empty string value is found in the XML document? Ideally, I would like to avoid implementing ISerializable because I don't really feel like spending the rest of the day into the burning pit of hell (i.e. implementing ISerializable for about a hundred classes).
干杯!
让 - 米歇尔·
Cheers! Jean-Michel
推荐答案
您可以指定像
[XmlElement]
[System.ComponentModel.DefaultValueAttribute(0.0)]
public double AverageSpeed
{ ... }
/编辑:好吧,怪beaviour在这里。无论我设定为它总是域值的属性值:
/edit: ok, strange beaviour here. Whatever I set as value in the Attribute it's always the fields value:
private double averageSpeed = 2.0;
但没有异常发生。
But no exception occurs.
这篇关于使用强类型的XSD错误deserialising XML文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!