发出将xml从基类序列化到派生类的问题

时间:2011-08-09 20:44:00

标签: .net soap xml-serialization

我遇到了从基类类型数组序列化派生类的问题。 它在序列化时成功识别派生类,因为序列化元素名称反映派生类,但派生类的唯一属性不会与对象一起序列化。

基类:

[System.Xml.Serialization.XmlIncludeAttribute(typeof(ContactsFolderType))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(CalendarFolderType))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(FolderType))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(TasksFolderType))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(SearchFolderType))]
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://schemas.microsoft.com/exchange/services/2006/types")]
public abstract partial class BaseFolderType
{

    private FolderIdType folderIdField;

    private FolderIdType parentFolderIdField;

    private string folderClassField;

    private string displayNameField;

    private int totalCountField;

    private bool totalCountFieldSpecified;

    private int childFolderCountField;

    private bool childFolderCountFieldSpecified;

    private ExtendedPropertyType[] extendedPropertyField;

    private ManagedFolderInformationType managedFolderInformationField;

    private EffectiveRightsType effectiveRightsField;

    /// <remarks/>
    public FolderIdType FolderId
    {
        get
        {
            return this.folderIdField;
        }
        set
        {
            this.folderIdField = value;
        }
    }

    /// <remarks/>
    public FolderIdType ParentFolderId
    {
        get
        {
            return this.parentFolderIdField;
        }
        set
        {
            this.parentFolderIdField = value;
        }
    }

    /// <remarks/>
    public string FolderClass
    {
        get
        {
            return this.folderClassField;
        }
        set
        {
            this.folderClassField = value;
        }
    }

    /// <remarks/>
    public string DisplayName
    {
        get
        {
            return this.displayNameField;
        }
        set
        {
            this.displayNameField = value;
        }
    }

    /// <remarks/>
    public int TotalCount
    {
        get
        {
            return this.totalCountField;
        }
        set
        {
            this.totalCountField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool TotalCountSpecified
    {
        get
        {
            return this.totalCountFieldSpecified;
        }
        set
        {
            this.totalCountFieldSpecified = value;
        }
    }

    /// <remarks/>
    public int ChildFolderCount
    {
        get
        {
            return this.childFolderCountField;
        }
        set
        {
            this.childFolderCountField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool ChildFolderCountSpecified
    {
        get
        {
            return this.childFolderCountFieldSpecified;
        }
        set
        {
            this.childFolderCountFieldSpecified = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("ExtendedProperty")]
    public ExtendedPropertyType[] ExtendedProperty
    {
        get
        {
            return this.extendedPropertyField;
        }
        set
        {
            this.extendedPropertyField = value;
        }
    }

    /// <remarks/>
    public ManagedFolderInformationType ManagedFolderInformation
    {
        get
        {
            return this.managedFolderInformationField;
        }
        set
        {
            this.managedFolderInformationField = value;
        }
    }

    /// <remarks/>
    public EffectiveRightsType EffectiveRights
    {
        get
        {
            return this.effectiveRightsField;
        }
        set
        {
            this.effectiveRightsField = value;
        }
    }
}

派生类:

[System.Xml.Serialization.XmlIncludeAttribute(typeof(TasksFolderType))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(SearchFolderType))]
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://schemas.microsoft.com/exchange/services/2006/types")]
public partial class FolderType : BaseFolderType
{

    private PermissionSetType permissionSetField;

    private int unreadCountField;

    private bool unreadCountFieldSpecified;

    /// <remarks/>
    public PermissionSetType PermissionSet
    {
        get
        {
            return this.permissionSetField;
        }
        set
        {
            this.permissionSetField = value;
        }
    }

    /// <remarks/>
    public int UnreadCount
    {
        get
        {
            return this.unreadCountField;
        }
        set
        {
            this.unreadCountField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool UnreadCountSpecified
    {
        get
        {
            return this.unreadCountFieldSpecified;
        }
        set
        {
            this.unreadCountFieldSpecified = value;
        }
    }
}

将此类添加到BaseFolderType数组后,序列化元素将返回

<Folder>
<FolderId/>
<DisplayName/>
...
 </Folder>
All the base properties but none of the derived ones.

我是否遗漏了我的xml属性设置导致此问题?

1 个答案:

答案 0 :(得分:-1)

请看一下这篇文章。我相信它会回答你的问题:

http://msdn.microsoft.com/en-us/library/aa302290.aspx