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();
When your xml is parsed it gives the following DOM tree:
Original xml:
Generated DOM tree:
So the carriage returns are in the XML document.
When i use an
MXXMLWriter
to convert the XML back into text, i get following XML fragment:So you can get your original XML, with all the embedded whitespace and linebreaks.
The issue you're having is that you want to display the XML as HTML. There are a number of issues with that.
If you blindly try to output the XML inside HTML:
Then nothing appears. The html parser doesn't recognize the
<comment>
element. Browsers are required to not render any elements they don't recognize.The first fix one might try, is escape the
<
and>
characters, as you are required to do, with<
and>
:This now renders in a browser, but renders as:
The reason for this is that the browser is required to collapse whitespace (e.g, tab, space, linebreak) into a single space.
There are two ways you can handle this. First is to put the "pre-formatted" xml into a preformatted (
PRE
) html element:This gives the output in HTML:
The other, more complicated, solution is to work with HTML, and present the HTML you wish. If you want to maintain all the whitespace (and not have the browser collapse it), then you must convert it explicitly into non-breaking space characters, by replacing "
" with "
":And you also must expliditely have line breaks by converting every "
CRLF
" into "<BR>
":And now you have HTML that preserves your spaces and carriage returns:
But can XSL do it?
i don't know.
XSL may be an NP Turing-complete language, but i don't know if it can:
finally i found the solution the solution is to replace xml space to html space after html generated i add this
at the end of the method before closing the stream reader
This transformation:
when applied on the provided XML document:
produces an HTML result with the wanted properties:
and it displays in the browser as:
sdagsg
fag
fdhfdhgf
and if you want to preserve even the whitespace, this can be done by a slight modification of the above transformation:
The result now has every space character replaced by a non-breaking space and in the browser it displays as:
sdagsg
fag
fdhfdhgf
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.