如何使用json.net使用XmlIgnore序列化属性

时间:2015-03-27 12:50:47

标签: json json.net xmlignore

我有一个模型,我以json和xml的形式检索。

我希望为xml seralize Json的不同属性。

[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 }

但我有&#34; IgnoreForXml&#34;财产是

{ "IncludeAlways":true }

使用.Net Framework 2.0 Newtonsoft JSON.Net for json和.Net Framework 2.0标准XmlSerializer for xml。 xml是作为WebMethod的结果而生成的,我使用标准的XmlSerializer序列化为xml。

0 个答案:

没有答案