none
How to set break line or special symbol in XML? RRS feed

  • Question

  • Currently, I got a problem with text in XML.

    private readonly string htmlTemplate = "<![CDATA[{0}]]>";
    
    string abc = string.Format(htmlTemplate,classsificationDetailItem.Description.Replace(@"•", string.Empty)
                                        .Replace(@"<", "&lt;")
                                        .Replace(@">", "&gt")
                                        .Replace(@"≤", "&le;")
                                        .Replace(@"≤", "&le;")
                                        .Replace(@"≥", "&ge;"))
    
    
    
    
    
                    string docText = null;
                    using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
                    {
                        docText = sr.ReadToEnd();
                    }
    
    
                   docText = docText.Replace("special symbol", abc);
    
                    using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
                    {
                        sw.Write(docText);
                    }


                

    In class, I parse a text to XML and download it but it's incorrect. The text still display as the example is bellow

    3 &lt; 4 &le;

    .....";

    I want to break a line and set special symbols to string and parse them to XML.


    Wednesday, August 29, 2018 2:58 AM

All replies

  • You can use HTML <BR> tag for line break. In your case - you can use like 

    &lt;br&gt;


    Thanks,
    Vivek Bansod
    Blog | MSDN | LinkedIn
     

    Wednesday, August 29, 2018 10:29 AM
  • What are you trying to do exactly with that XML? It looks like you're trying to fix CDATA sections but that probably isn't correct. CDATA is designed to hold any content (valid XML or otherwise). It is most useful for storing things like code inside XML without the need for encoding.

    If you want to read CDATA sections I'd recommend loading the XML up into an XDocument. In your code I'm not convinced you're actually finding any CDATA sections because you're looking for CDATA??? but that wouldn't be a valid HTML CDATA section.

    Using XDocument you can easily find the CDATA sections. For each one you can then get the raw text. Once you have the raw text, if you really want to try to HTML encode it then you can use HtmlEncode or similar to encode the HTML characters. You can also do any non-HTML conversions like converting newlines to HTML breaks (with String.Replace). Once you're done save the changes back and you're done.



    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, August 29, 2018 2:03 PM
    Moderator