1

我对 .NET XML 序列化有疑问。如果我在没有特定标签的情况下反序列化 XML 并将此对象序列化回 XML,其中一些标签将被跳过。无参数构造函数将这些属性设置为默认值,并且它们同时具有 getter 和 setter。

只有int属性被跳过,String被序列化。

输入文件:

<FileList>
  <File>
    <FilePath>RE.msg</FilePath>
  </File>
</FileList>

输出文件:

<FileList GUID="A13EDDDF-F55A-4540-A776-609245B92D83" Created="2012-08-23T09:46:30+03:00">
  <File GUID="28C6E090-A3D5-4CD7-97A6-7A46F2F0DDF7" Created="2012-08-23T09:46:30+03:00">
    <FilePath>RE.msg</FilePath>
    <FileStamp>wjoeao1x.msg</FileStamp>
    <StatusMessage>Created</StatusMessage>
  </File>
</FileList>

int无参数构造函数为属性设置值,Status但它没有被序列化:

public CT_File()
{
    Status = 1;
    StatusMessage = "Created";
}

类定义。它是从 XSD 架构自动生成的。

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.1432")]
[System.SerializableAttribute()]
//[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class CT_File : CT_Base {

    private string filePathField;

    private string descriptionField;

    private string fileStampField;

    private string kapprisIDField;

    private int statusField;

    private bool statusFieldSpecified;

    private string statusMessageField;

    private int errorCodeField;

    private bool errorCodeFieldSpecified;

    private string errorMessageField;

    private CT_FileList attachmentsField;

    private string originalIDField;

    public CT_File()
    {
        Status = 1;
        StatusMessage = "Created";
        this.attachmentsField = new CT_FileList();
    }

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

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

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

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

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

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

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

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

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

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

    /// <remarks/>
    public CT_FileList Attachments {
        get {
            return this.attachmentsField;
        }
        set {
            this.attachmentsField = value;
        }
    }

    /// <remarks/>
    public string OriginalID {
        get {
            return this.originalIDField;
        }
        set {
            this.originalIDField = value;
        }
    }
}
4

1 回答 1

4

XmlSerializer 仅序列化公共字段/属性,并且对于属性:仅当它们同时具有 getter 和 setter 时。例如:

private int Status;

没有序列化,也没有:

private int status;
public int Status { get { return status; } }

或者:

private int status;
public int Status { get { return status; } private set { status = value; } }

或者:

public int Status { get; protected set; }

此外,如果成员被标记[XmlIgnore],或者如果存在与基于名称的模式匹配的方法,则不会序列化成员:

public bool ShouldSerializeStatus() { return false; }

第二种模式的工作方式相同,但允许双向检查(即“我应该序列化这个吗?”和“这在反序列化期间是否得到了一个值?”):

[XmlIgnore]
public bool StatusSpecified { get {...} set {...} }

此外,与其显式默认值匹配的值不会被序列化:

[DefaultValue(1)]
public int Status { get; set; } // won't serialize if ===1

我的猜测是,由于上述原因之一,它没有序列化。

于 2012-08-23T07:12:00.627 回答