简体   繁体   English

使用asp.net从xml文件写入/读取时保留回车

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

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 我有TextBox来取消用户的评论,评论会被保存到XML文件中,问题是我写文字时输入密钥(新行)它会以正确的方式保存到xml中这样

            <comment>
              sdagsg
               fag
                fdhfdhgf
              </comment>

but when i read from the xml looks like this " sdagsg fag fdhfdhgf" 但是当我从xml读取时看起来像这样的“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();

There is nothing wrong with the read of the XML file. 读取XML文件没有任何问题。 XML is not sensitive to white space. XML对空白区域不敏感。

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. 当您希望XML中的某些部分不遵循所有XML规则并且有点特殊时,您可以使用所谓的CDATA部分。 This is what you should be using when "saving" the data of the user. 这是“保存”用户数据时应该使用的内容。

See one way of how to do it in C#. 请参阅如何在C#中执行此操作的一种方法。 The way you write XML may have a different equivalent: 您编写XML的方式可能有不同的等价物:
http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.createcdatasection.aspx 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. 我认为我同意Davide Piras对这个问题的评论。 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. 我认为你遇到了与使用XmlDocument转义换行符相同的问题,因此从那里选择了我最喜欢的答案。

When your xml is parsed it gives the following DOM tree: 解析xml时,它会提供以下DOM树:

Original xml: 原始xml:

            <comment>
              sdagsg
               fag
                fdhfdhgf
              </comment>

Generated DOM tree: 生成的DOM树:

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

So the carriage returns are in the XML document. 所以回车 XML文件内。

When i use an MXXMLWriter to convert the XML back into text, i get following XML fragment: 当我使用MXXMLWriter将XML转换回文本时,我得到以下XML片段:

<comment>
              sdagsg
               fag
                fdhfdhgf
              </comment>

So you can get your original XML, with all the embedded whitespace and linebreaks. 所以,你可以得到你的原始XML,所有的嵌入式空格和换行。

The issue you're having is that you want to display the XML as HTML. 您遇到的问题是您希望将XML显示为HTML。 There are a number of issues with that. 这有很多问题。

If you blindly try to output the XML inside HTML: 如果你盲目地尝试在HTML中输出XML:

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

Then nothing appears. 然后没有出现。 The html parser doesn't recognize the <comment> element. html解析器无法识别<comment>元素。 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 &lt; 第一个修复程序可能会尝试, 正如您所要求的那样 ,使用&lt;转义<>字符&lt; and &gt; &gt; :

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

This now renders in a browser, but renders as: 这现在在浏览器中呈现,但呈现为:

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

The reason for this is that the browser is required to collapse whitespace (eg, 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: 首先是将“预格式化”的xml放入预先格式化的PRE )html元素中:

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

This gives the output in HTML: 这给出了HTML输出:

<comment>
                  sdagsg
                   fag
                    fdhfdhgf
                  </comment>

The other, more complicated, solution is to work with HTML, and present the HTML you wish. 另一个更复杂的解决方案是使用 HTML,并呈现您想要的HTML。 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 " &nbsp; “with” &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>

And you also must expliditely have line breaks by converting every " CRLF " into " <BR> ": 而且你还必须通过将每个“ 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>

And now you have HTML that preserves your spaces and carriage returns: 现在你有了HTML来保存你的空格和回车:

<comment> <评论>
sdagsg sdagsg
fag FAG
fdhfdhgf fdhfdhgf
</comment> </评论>


But can XSL do it? 但是XSL能做到吗?

i don't know. 我不知道。

XSL may be an NP Turing-complete language, but i don't know if it can: XSL可能是NP图灵完备语言,但我不知道它是否可以:

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

This transformation : 这种转变

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

when applied on the provided XML document : 当应用于提供的XML文档时

<comment>
   sdagsg
     fag
      fdhfdhgf
</comment>

produces an HTML result with the wanted properties : 生成包含所需属性的HTML结果

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

and it displays in the browser as : 它在浏览器中显示为


sdagsg sdagsg
fag FAG
fdhfdhgf fdhfdhgf

and if you want to preserve even the whitespace, this can be done by a slight modification of the above transformation : 如果你想保留甚至空白,可以通过略微修改上述转换来完成

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

The result now has every space character replaced by a non-breaking space and in the browser it displays as : 结果现在将每个空格字符替换为不间断的空格,并在浏览器中显示为


sdagsg sdagsg
fag FAG
fdhfdhgf fdhfdhgf

finally i found the solution the solution is to replace xml space to html space after html generated i add this 最后我发现解决方案的解决方案是在生成html之后将xml空间替换为html空间我添加此项

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

at the end of the method before closing the stream reader 在关闭流阅读器之前的方法结束时

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM