preserve回车当我写/从XML文件中使用asp.net读取 [英] Preserve carriage returns when i write/read from xml file using asp.net

查看:79
本文介绍了preserve回车当我写/从XML文件中使用asp.net读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有文本框接受来自用户的评论,评论将被保存到XML文件
问题是,当我写的文本具有回车键(新行),它会保存到XML以正确的方式
像这样

 <注释和GT;
              sdagsg
               FAG
                fdhfdhgf
              < /评论>

但是当我从XML阅读
看起来是这样的sdagsg FAG fdhfdhgf

 字符串strXstFile =使用Server.Mappath(@〜/ TopicAndComments.xsl);
        XslCompiledTransform X =新XslCompiledTransform();        //加载XML
        XPathDocument的文档=新的XPathDocument(使用Server.Mappath(@〜/ TopicAndComments.xml));        //加载样式表。
        XslCompiledTransform XSLT =新XslCompiledTransform();
        xslt.Load(strXstFile);        MemoryStream的毫秒=新的MemoryStream();
        XmlTextWriter的作家=新的XmlTextWriter(MS,Encoding.ASCII);
        StreamReader的RD =新的StreamReader(毫秒);
        //通行证的主题ID到XSL文件
        的XsltArgumentList xslArg =新的XsltArgumentList();
        xslArg.AddParam(TopicID,,HiddenField_SelectedTopicID.Value.ToString());
        xslt.Transform(DOC,xslArg,作家);        ms.Position = 0;
        strHTML中= rd.ReadToEnd();
        rd.Close();
        ms.Close();


解决方案

有什么毛病XML文件的读取。 XML是不是空白敏感。

当你想在XML一些地方没有遵循所有的XML规则,并且有点特别,您使用的是所谓的CDATA部分。这是你应该当节能的用户的数据使用什么。

请参阅如何做到在C#中的一种方式。你写XML可以有不同的等价方式:结果
http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.createcdatasection.aspx

我想我在他对这个问题的评论与达维德Piras酒店同意。我觉得你有同样的问题作为<一个href=\"http://stackoverflow.com/questions/5160632/escaping-new-line-characters-with-xmldocument\">Escaping与XmlDocument的换行字符,因此从那里挑最喜欢的答案给我。

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();

解决方案

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.

这篇关于preserve回车当我写/从XML文件中使用asp.net读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆