通过ASMX Web服务进行序列化时保持空格/换行符

时间:2021-10-31 09:14:08

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.

我正在ASMX Webservice(旧版.NET SOAP服务)中对XML文档进行一些预处理,以便最终在Silverlight前端使用。

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

我正在将该XML文档处理为POCO对象以便于使用。该对象定义如下:

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
}

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属性包含基本格式化的文本(换行符,空格等等)。提供该属性的XML节点如下所示:

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

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?

一切都很好,格式保持正如我所期望的那样,Web服务序列化要发送到前端的数据。我猜测在尝试优化带宽时,序列化对象会在发送之前从Text属性中删除额外的空格和换行符。在这个特定的例子中,格式化很重要。有没有办法强制Web服务维护这个空格/换行格式?

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.

我想我编码代替了一些编码来讨论有问题的项目,然后转换回前端,但这让我觉得有点像kludge。

2 个解决方案

#1


You can serialize it as a CDATA section :

您可以将其序列化为CDATA部分:

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

#2


I presume you're referring to ASMX web services?

我认为你指的是ASMX网络服务?

Actually, I don't think there's a way to do this without serializing as a byte array. Alternatively, you could implement IXmlSerializable and do all the work yourself.

实际上,我认为如果没有作为字节数组进行序列化,就有办法做到这一点。或者,您可以实现IXmlSerializable并自己完成所有工作。


You'll want something like this:

你会想要这样的东西:

public class CACDocument : ITextDocument {
    // ...
    [XmlIgnore]
    public string Text {get;set;}

    [XmlText]
    public byte[] TextSubstitute {
        get {return System.Text.Encoding.UTF8.GetBytes(Text);}
        set {Text = System.Text.Encoding.UTF8.GetString(value);}
    }
}

That's not tested, but you'll get the idea. You can also use [XmlElement] instead of [XmlText], and specify a different element name.

这没有经过测试,但你会得到这个想法。您也可以使用[XmlElement]而不是[XmlText],并指定不同的元素名称。

#1


You can serialize it as a CDATA section :

您可以将其序列化为CDATA部分:

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

#2


I presume you're referring to ASMX web services?

我认为你指的是ASMX网络服务?

Actually, I don't think there's a way to do this without serializing as a byte array. Alternatively, you could implement IXmlSerializable and do all the work yourself.

实际上,我认为如果没有作为字节数组进行序列化,就有办法做到这一点。或者,您可以实现IXmlSerializable并自己完成所有工作。


You'll want something like this:

你会想要这样的东西:

public class CACDocument : ITextDocument {
    // ...
    [XmlIgnore]
    public string Text {get;set;}

    [XmlText]
    public byte[] TextSubstitute {
        get {return System.Text.Encoding.UTF8.GetBytes(Text);}
        set {Text = System.Text.Encoding.UTF8.GetString(value);}
    }
}

That's not tested, but you'll get the idea. You can also use [XmlElement] instead of [XmlText], and specify a different element name.

这没有经过测试,但你会得到这个想法。您也可以使用[XmlElement]而不是[XmlText],并指定不同的元素名称。