我有一个模型,我检索为 json 和 xml。
我想为 Json 为 xml 序列化不同的属性。
[Serlializable]
class Model
{
public bool? IncludeAlways {get;set;}
[XmlIgnore]
public bool? IncludeForJson {get;set;}
[JsonIgnore]
public bool? IncludeForXml {get;set;}
}
样品用途:
new Model()
{
IncludeAlways = true,
IncludeForJson = true,
IncludeForXml = true
};
它产生预期的 Xml:
<Model>
<IncludeAlways>true</IncludeAlways>
<IncludeForXml>true</IncludeForXml>
</Model>
但不是预期的json
我期望一些json是这样的:
{ "IncludeAlways":true, "IncludeForJson":true }
但我有“IgnoreForXml”属性是
{ "IncludeAlways":true }
对 json 使用 .Net Framework 2.0 Newtonsoft JSON.Net,对 xml 使用 .Net Framework 2.0 标准 XmlSerializer。xml 是作为 WebMethod 的结果生成的,我假设它是使用序列化为 xml 的标准 XmlSerializer 序列化的。