Carriage save returns when I write / read from an xml file using asp.net

I have a TextBox for comments from the user, the comments will be saved in an XML file, the problem is that when I write the text, enter the key (new line), which it will save in xml correctly like this

            <comment>
              sdagsg
               fag
                fdhfdhgf
              </comment>

but when I read from xml it 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();
+5
source share
4 answers

There is nothing wrong with reading an XML file. XML is not space sensitive.

, XML XML , CDATA. , , "" .

, #. XML :
http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.createcdatasection.aspx

, . , , " XmlDocument" , , .

+2

XML , DOM:

xml:

            <comment>
              sdagsg
               fag
                fdhfdhgf
              </comment>

DOM:

|- NODE_DOCUMENT #document ""
  |- NODE_ELEMENT comment ""
    |- NODE_TEXT #text "\n              sdagsg\n               fag\n                fdhfdhgf\n              "

, XML.

MXXMLWriter, XML , XML:

<comment>
              sdagsg
               fag
                fdhfdhgf
              </comment>

, XML .

, , , XML HTML. .

XML HTML:

<P><comment>
              sdagsg
               fag
                fdhfdhgf
              </comment></P>

. html <comment>. - , .

, , - < >, , &lt; &gt;:

<P>&lt;comment&gt;
              sdagsg
               fag
                fdhfdhgf
              &lt;/comment&gt;</P>

, :

< > sdagsg fag fdhfdhgf </comment>

, (, , , linebreak) .

. " " xml (PRE) html:

<PRE>&lt;comment&gt;
                  sdagsg
                   fag
                    fdhfdhgf
                  &lt;/comment&gt;</PRE>

HTML:

<comment>
                  sdagsg
                   fag
                    fdhfdhgf
                  </comment>

, HTML HTML, . ( ), , "" "&nbsp;":

<P>&lt;comment&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sdagsg
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fag
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fdhfdhgf
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/comment&gt;</P>

, "CRLF" "<BR>":

<P>&lt;comment&gt;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sdagsg<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fag<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fdhfdhgf<BR>

HTML, :

< >
                  sdagsg
                   
                    fdhfdhgf
                  </ >


XSL ?

.

XSL , , :

String html = DocumentToString(xmlDocument);
html = StringReplace(html, "<", "&lt;");
html = StringReplace(html, ">", "&gt;");
html = StringReplace(html, " ", "&nbsp;");
html = StringReplace(html, "\r\n", "<BR>");
0

:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="text()" name="replaceNL">
   <xsl:param name="pText" select="."/>

         <xsl:choose>
           <xsl:when test="not(contains($pText, '&#10;'))">
            <xsl:copy-of select="$pText"/>
           </xsl:when>
           <xsl:otherwise>
             <xsl:copy-of select="substring-before($pText, '&#10;')"/>
             <br />
             <xsl:call-template name="replaceNL">
               <xsl:with-param name="pText" select=
                "substring-after($pText, '&#10;')"/>
             </xsl:call-template>
           </xsl:otherwise>
         </xsl:choose>
 </xsl:template>
</xsl:stylesheet>

XML-:

<comment>
   sdagsg
     fag
      fdhfdhgf
</comment>

HTML :

<br/>   sdagsg<br/>     fag<br/>      fdhfdhgf<br/>

, :


sdagsg
fag
fdhfdhgf

, , :

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="text()" name="replaceNL">
   <xsl:param name="pText" select=
    "translate(., ' ', '&#160;')"/>

         <xsl:choose>
           <xsl:when test="not(contains($pText, '&#10;'))">
            <xsl:copy-of select="$pText"/>
           </xsl:when>
           <xsl:otherwise>
             <xsl:copy-of select="substring-before($pText, '&#10;')"/>
             <br />
             <xsl:call-template name="replaceNL">
               <xsl:with-param name="pText" select=
                "substring-after($pText, '&#10;')"/>
             </xsl:call-template>
           </xsl:otherwise>
         </xsl:choose>
 </xsl:template>
</xsl:stylesheet>

, :


sdagsg

fdhfdhgf

0

, xml html- html.

    strHtml = strHtml.Replace("&lt;br/&gt;", "<br/>");

0

All Articles