通过ASMX Web服务序列化时,保持空白/换行符 [英] Maintain whitespace/line breaks when serializing via ASMX Web Service

查看:209
本文介绍了通过ASMX Web服务序列化时,保持空白/换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做在Silverlight前端部分pre-处理一个ASMX web服务(传统.NET SOAP服务)的最终使用的XML文档。

I'm doing some pre-processing of an XML document in a ASMX Webservice (Legacy .NET SOAP Service) for eventual use in a Silverlight front end.

我处理的XML文档转换成易于使用的一个POCO对象。对象的定义如下:

I'm processing that XML document into a POCO object for ease of use. The object is defined as follows:

public class CACDocument : ITextDocument
{
    #region Properties
    public string Title { get; set; }
    public string Text { get; set; }
    public List<Code> CodeList { get; set; }
    public XElement FormatedText { get; set; }
    #endregion

    #region Constructor
    public CACDocument()
    {
        CodeList = new List<Code>();
    }
    #endregion
}

在该对象的Text属性包含基本格式文本(换行,空格等)。该供稿属性的XML节点如下:

The Text property in that object contains basically formatted text (line breaks, white space, etc...). The XML node that feeds that property looks like this:

<text>
   A TITLE FOLLOWED BY two line breaks


   Some text followed by a line break

   Some more text that might extend for a paragraph or two followed by more line breaks

   Still more text
</text>

所有的罚款和格式保持为我期望了单元1 Web服务序列化到被发送到前端的数据。我猜,企图以优化带宽,序列化对象将其发送出去之前,从剥离的Text属性的额外的空格和换行。在这一特定情况下,该格式是非常重要的。有没有办法强制web服务,以维持这个空白/换行格式?

All is fine and format is maintained as I would expect up unitl the Web Services serializes the data to be sent out to the front end. I'm guessing that in an attempt to optimize bandwidth, the serialized object strips the extra whitespace and line breaks from the Text property before sending it out. In this one particular instance, that formatting is important. Is there a way to force the webservice to maintain this whitespace/line break formatting?

我想,我code代替一些编码有问题的项目,然后再转换回在前端,但令我有点杂牌的。

I imagine that I code substitute some coding for the items in question, and then convert back on the front end, but that strikes me as a bit of a kludge.

推荐答案

您可以把它作为一个CDATA部分序列:

You can serialize it as a CDATA section :

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

    private static readonly XmlDocument _xmlDoc = new XmlDocument();

    [XmlElement("Text")]
    public XmlCDataSection TextCData
    {
        get
        {
            return _xmlDoc.CreateCDataSection(Text);
        }
        set
        {
            Text = value.Data;
        }
    }

文本将被序列化这样的:

The text will be serialized like that :

<text><![CDATA[A TITLE FOLLOWED BY two line breaks


   Some text followed by a line break

   Some more text that might extend for a paragraph or two followed by more line breaks

   Still more text]]></text>

这篇关于通过ASMX Web服务序列化时,保持空白/换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆