我正在ASMX Webservice(旧版.NET SOAP服务)中对 XML文档进行一些预处理,以便最终在Silverlight前端使用. 我正在将该XML文档处理为POCO对象以便于使用.该对象定义如下: public class CACDocument : ITex
我正在将该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 }
该对象中的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>
一切都很好,格式保持正如我所期望的那样,Web服务序列化要发送到前端的数据.我猜测在尝试优化带宽时,序列化对象会在发送之前从Text属性中删除额外的空格和换行符.在这个特定的例子中,格式化很重要.有没有办法强制Web服务维护这个空格/换行格式?
我想我的代码替换了一些编码来解决有问题的项目,然后转换回前端,但这让我觉得有点像kludge.
您可以将其序列化为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; } }
文本将被序列化为:
<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>