gpt4 book ai didi

c# - 当我使用 asp.net 从 xml 文件写入/读取时保留回车符

转载 作者:数据小太阳 更新时间:2023-10-29 01:58:40 26 4
gpt4 key购买 nike

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

最佳答案

读取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 相同的问题,因此从那里选择了我最喜欢的答案。

关于c# - 当我使用 asp.net 从 xml 文件写入/读取时保留回车符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7292407/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com