我的.NET XML序列化存在問題。 如果我在沒有特定標籤的情況下反序列化XML,並將這個對象序列化回XML,那麼這些標籤中的一些將被跳過。無參數構造函數將這些屬性設置爲默認值,並且它們同時具有getter和setter。如果未初始化,.NET XML序列化標記丟失
只有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;
}
}
}
我們可以看到與「Status」和「StatusMessage」有關的屬性/字段嗎? –