Writing CDATA in ASP.NET WebApi

烂漫一生 提交于 2019-12-13 07:07:22

问题


Im using the setting:

formatters.XmlFormatter.UseXmlSerializer = true;

The class i try to serailize is quite simple:

public class MyClass
{
    public MyClass()
    {
        CDATA = "<![CDATA[<link>MyLink</link>]]>"

    [XmlText]
    public string CDATA { get; set; }
}

I want this to be serialized into something like:

<MyClass>
     <![CDATA[<link>MyLink</link>]]>
</MyClass>

But instead get:

<MyClass>
     &lt;![CDATA[&lt;!link&gt;MyLink&lt;!/link&gt;]]>
</MyClass>

So how can i prevent this? Or is there a better way using the ASP.NET WebApi?


回答1:


Looks like the answer from this question will do it:

[XmlIgnore] public string Content { get; set; }

[XmlText]
public XmlNode[] CDataContent {
    get {
        return new XmlNode[] {
            new XmlDocument().CreateCDataSection(Content)
        };
    }
    set { Content = value[0].Value; }
}

This works with a regular XmlSerializer object, so I'd guess it works in WebAPI as well.



来源:https://stackoverflow.com/questions/18067242/writing-cdata-in-asp-net-webapi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!