序列化派生类时,具有基类的属性序列化

时间:2011-09-07 23:27:35

标签: c# xml silverlight serialization

我有一个带有一些属性的基类:

    // must include any derived classes here as known types or else they will throw errors on serialization
[KnownType(typeof(CollaborationEventMeasureDistance))]
[DataContract]
public partial class CollaborationEvent
{
    public bool HasBeenTransported { get; set; }
    public Guid MessageBoxGuid { get; set; } 

    public CollaborationEvent()
    {
        HasBeenTransported = false;
    }

}

一个派生类,它有一些属性:

    public class CollaborationEventMeasureDistance : CollaborationEvent
{
    public Geometry Geometry { get; set; }
}

当我序列化派生类时,它的所有属性都是序列化的,但它从基类继承的属性不是:

<CollaborationEvent i:type="CollaborationEventMeasureDistance">
  <Geometry xmlns:d4p1="http://schemas.datacontract.org/2004/07/ESRI.ArcGIS.Client.Geometry"
  i:type="d4p1:Polyline">
    <d4p1:spatialReference>
      <d4p1:wkid>26910</d4p1:wkid>
    </d4p1:spatialReference>
    <d4p1:paths>
      <d4p1:points>
        <d4p1:point>
          <d4p1:spatialReference>
            <d4p1:wkid>26910</d4p1:wkid>
          </d4p1:spatialReference>
          <d4p1:x>460892.23924271885</d4p1:x>
          <d4p1:y>5367682.5572773879</d4p1:y>
        </d4p1:point>
        <d4p1:point>
          <d4p1:spatialReference i:nil="true" />
          <d4p1:x>461001.35841108358</d4p1:x>
          <d4p1:y>5367648.5755294543</d4p1:y>
        </d4p1:point>
      </d4p1:points>
    </d4p1:paths>
  </Geometry>
</CollaborationEvent>

有谁可以指出我做错了什么?

我希望我的XML看起来更像:

    <CollaborationEvent i:type="CollaborationEventMeasureDistance">
  <HasBeenTransported>True</HasBeenTransported>
  <MessageBoxGuid>blah</MessageBoxGuid>
  <Geometry xmlns:d4p1="http://schemas.datacontract.org/2004/07/ESRI.ArcGIS.Client.Geometry"
  i:type="d4p1:Polyline">
    <d4p1:spatialReference>
      <d4p1:wkid>26910</d4p1:wkid>
    </d4p1:spatialReference>
    <d4p1:paths>
      <d4p1:points>
        <d4p1:point>
          <d4p1:spatialReference>
            <d4p1:wkid>26910</d4p1:wkid>
          </d4p1:spatialReference>
          <d4p1:x>460892.23924271885</d4p1:x>
          <d4p1:y>5367682.5572773879</d4p1:y>
        </d4p1:point>
        <d4p1:point>
          <d4p1:spatialReference i:nil="true" />
          <d4p1:x>461001.35841108358</d4p1:x>
          <d4p1:y>5367648.5755294543</d4p1:y>
        </d4p1:point>
      </d4p1:points>
    </d4p1:paths>
  </Geometry>
</CollaborationEvent>

由于

1 个答案:

答案 0 :(得分:2)

假设您的Geometry类是可序列化的,请尝试以下方法:

[DataContract, Serializable]
public class CollaborationEventMeasureDistance : CollaborationEvent  
{  
    [DataMember]
    public Geometry Geometry { get; set; }  
} 

[KnownType(typeof(CollaborationEventMeasureDistance))] 
[DataContract, Serializable]
public partial class CollaborationEvent 
{ 
    [DataMember]
    public bool HasBeenTransported { get; set; } 
    [DataMember]
    public Guid MessageBoxGuid { get; set; }  

    public CollaborationEvent() 
    { 
        HasBeenTransported = false; 
    } 

}