我遇到.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;
}
}
}
答案 0 :(得分: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
我的猜测是,由于上述原因之一,它没有序列化。