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

时间:2011-09-03 09:53:40

标签: c# asp.net xml xslt

我有TextBox来收集用户的评论,评论将保存到XML文件中 问题是,当我写一个文本有输入键(新行)时,它将以正确的方式保存到xml中 像这样

            <comment>
              sdagsg
               fag
                fdhfdhgf
              </comment>

但是当我从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();

4 个答案:

答案 0 :(得分:2)

读取XML文件没有任何问题。 XML对空白区域不敏感。

当您希望XML中的某些部分不遵循所有XML规则并且有点特殊时,您使用所谓的CDATA部分。这是“保存”用户数据时应该使用的。

在C#中查看如何执行此操作的一种方法。您编写XML的方式可能有不同的等价物:
http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.createcdatasection.aspx

我认为我同意Davide Piras对这个问题的评论。我认为你和Escaping new-line characters with XmlDocument有同样的问题,因此从那里选择了我最喜欢的答案。

答案 1 :(得分:0)

解析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。这有很多问题。

如果你盲目地尝试在HTML中输出XML:

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

然后什么也没出现。 html解析器无法识别<comment>元素。浏览器不得渲染任何他们无法识别的元素。

可以尝试的第一个修复方法是使用<>转义&lt;&gt;字符,as you are required to do

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

现在在浏览器中呈现,但呈现为:

  

&LT;注释和GT; sdagsg fag fdhfdhgf&lt; / comment&gt;

原因是浏览器需要将空格(例如,制表符,空格,换行符)折叠到一个空格中。

有两种方法可以解决这个问题。首先是将“预格式化”的xml放入预先格式化的PRE)html元素中:

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

这以HTML格式输出:

<comment>
                  sdagsg
                   fag
                    fdhfdhgf
                  </comment>

另一个更复杂的解决方案是使用 HTML ,并呈现您想要的HTML。如果您想维护所有空格(并且浏览器没有将其折叠),则必须将“”替换为“&nbsp;”,然后将其明确转换为不间断的空格字符:< / p>

<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来保存你的空格和回车:

  

&LT;注释和GT;
  sdagsg
  FAG
  fdhfdhgf
  &LT; /注释&GT;


但是XSL能做到吗?

我不知道。

XSL可能是NP图灵完备语言,但我不知道它是否可以:

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

答案 2 :(得分: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
FAG
fdhfdhgf

答案 3 :(得分:0)

最后我找到了解决方案 解决方法是在html生成后添加此

,将xml空间替换为html空间
    strHtml = strHtml.Replace("&lt;br/&gt;", "<br/>");
关闭流阅读器之前

在方法结束时