Preserve spaces / line breaks during serialization via ASMX web service

I am doing preprocessing of an XML document in the ASMX Webservice (Legacy.NET SOAP Service) for possible use in the Silverlight interface.

I am processing this XML document into a POCO object for ease of use. An 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
}

The Text property in this object contains mostly formatted text (line breaks, space, etc.). The XML node that passes this property is as follows:

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

, , , - , . , , , Text . . - /?

, , , kludge.

+3
2

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

, - ASMX?

, , . , IXmlSerializable .


- :

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);}
    }
}

, . [XmlElement] [XmlText] .

+2

Source: https://habr.com/ru/post/1712195/


All Articles