Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preserve carriage returns when i write/read from xml file using asp.net

Tags:

c#

xml

asp.net

xslt

i have TextBox to take comment from user, comments will be saved into XML file the problem is when i write a text have enter key (new line ) it will save into the xml in the right way like this

            <comment>
              sdagsg
               fag
                fdhfdhgf
              </comment>

but when i read from the xml looks like this " sdagsg fag fdhfdhgf"

           string strXstFile = Server.MapPath(@"~/TopicAndComments.xsl");
        XslCompiledTransform x = new XslCompiledTransform();

        // Load the XML 
        XPathDocument doc = new XPathDocument(Server.MapPath(@"~/TopicAndComments.xml"));

        // Load the style sheet.
        XslCompiledTransform xslt = new XslCompiledTransform();
        xslt.Load(strXstFile);

        MemoryStream ms = new MemoryStream();
        XmlTextWriter writer = new XmlTextWriter(ms, Encoding.ASCII);
        StreamReader rd = new StreamReader(ms);
        //Pass Topic ID to XSL file
        XsltArgumentList xslArg = new XsltArgumentList();
        xslArg.AddParam("TopicID", "", HiddenField_SelectedTopicID.Value.ToString());
        xslt.Transform(doc, xslArg, writer);

        ms.Position = 0;
        strHtml = rd.ReadToEnd();
        rd.Close();
        ms.Close();
like image 348
fatma Avatar asked Nov 04 '22 15:11

fatma


1 Answers

There is nothing wrong with the read of the XML file. XML is not sensitive to white space.

When you want some parts in XML to not follow all the XML rules and be a bit special, you use a so-called CDATA section. This is what you should be using when "saving" the data of the user.

See one way of how to do it in C#. The way you write XML may have a different equivalent:
http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.createcdatasection.aspx

And I think I agree with Davide Piras in his comment on the question. I think you are having the same issue as Escaping new-line characters with XmlDocument, and hence picked the favorite answer to me from there.

like image 84
Meligy Avatar answered Nov 09 '22 15:11

Meligy