gpt4 book ai didi

c# - 序列化 .NET 对象时缺少字段

转载 作者:行者123 更新时间:2023-11-30 14:17:27 26 4
gpt4 key购买 nike

我在使用 C# 序列化对象时遇到问题。当应用程序开始序列化对象时,某些字段会被序列化,而其他字段则不会。在以下代码中:

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ACORDInsuranceSvcRqHomePolicyQuoteInqRq
{

private string rqUIDField;

private System.DateTime transactionRequestDtField;

private System.DateTime transactionEffectiveDtField;

private string curCdField;

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

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

/// <remarks/>
[System.Xml.Serialization.XmlIgnore()]
public System.DateTime TransactionRequestDt
{
get
{
return this.transactionRequestDtField;
}
set
{
this.transactionRequestDtField = value;
}
}

/// <remarks/>
[XmlElement("TransactionRequestDt")]
public string TransactionRequestDtString
{
get
{
return String.Format("{0:yyyy-MM-dd}", this.TransactionRequestDt);
}
}

/// <remarks/>
[System.Xml.Serialization.XmlIgnore]
public System.DateTime TransactionEffectiveDt
{
get
{
return this.transactionEffectiveDtField;
}
set
{
this.transactionEffectiveDtField = value;
}
}

/// <remarks/>
[XmlElement("TransactionEffectiveDt")]
public string TransactionEffectiveDtString
{
get
{
return String.Format("{0:yyyy-MM-dd}", this.TransactionEffectiveDt);
}
}
}

您可以看到字段/访问器 RqUID 和 CurCd 被调用,但 TransactionRequestDtString 和 TransactionEffectiveDtString 没有被调用。我需要将所有这些序列化。谢谢!

最佳答案

如果需要对其进行 xml 序列化,则需要公共(public)获取和设置。

尝试将您的代码更改为:

[ReadOnly(true)]
[XmlElement("TransactionRequestDt")]
public string TransactionRequestDtString
{
get
{
return String.Format("{0:yyyy-MM-dd}", this.TransactionRequestDt);
}
set{}
}`

ReadOnly属性不会让任何人更改它。

关于c# - 序列化 .NET 对象时缺少字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5875771/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com