【问题标题】:C# XML serialization - float or empty elementC# XML 序列化 - 浮动或空元素
【发布时间】:2015-05-11 07:30:32
【问题描述】:

我有带有商店列表的 xml。每个商店都有和节点。在其中一些是浮动的,在一些是空节点。所以它看起来像这样:

<shops>
  <shop>
    <lat>123.123</lat>
    <lon>123.123</lon>
  </shop>
  <shop>
    <lat null="true"/>
    <lon null="true"/>
</shop>
</shops>

我的反序列化这个xml的类是这样的

[XmlRoot("shops")]
public class ShopList
{
    [XmlElement("shop")]
    public Shop[] ShopArray { get; set; }

    public class Shop
    {
        [XmlElement("lat", IsNullable = true)]
        public float? Latitude { get; set; }

        [XmlElement(ElementName="lon", IsNullable=true)]
        public float? Longitude { get; set; }
    }
 }

但是当我反序列化 xml 时出现错误。你有什么线索吗?

【问题讨论】:

  • 您好,欢迎来到 SO。能否请您更具体一点。错误是什么?

标签: c# xml-serialization


【解决方案1】:
public bool DataSpecified 
{ 
   get { return !String.IsNullOrEmpty(Data); }
   set { return; } //The serializer requires a setter
}

你应该试试something like this

【讨论】:

    【解决方案2】:

    我认为你的元素中不应该有null="true",而应该有xsi:nil="true"

    <shops>
      <shop>
        <lat>123.123</lat>
        <lon>123.123</lon>
      </shop>
      <shop>
        <lat xsi:nil="true"/>
        <lon xsi:nil="true"/>
      </shop>
    </shops>
    

    【讨论】:

      猜你喜欢
      • 2013-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-02
      相关资源
      最近更新 更多