gpt4 book ai didi

c# - 在 XElement.Load 上保留\r\n

转载 作者:行者123 更新时间:2023-12-02 15:02:48 25 4
gpt4 key购买 nike

有没有办法可以在 XElement.Load() 上保留\r\n。

  TextReader reader = new StringReader(rawInputString);//rawInputString is just text in html format
XElement root = XElement.Load(reader);

编辑:

string  rawInputString =  "<Html><Body><p></p> \r\n <p></p>\r\n \r\n \r\n </Body></Html>";

在 XELement.Load 之后,我想要的输出与我执行 root.ToString() 时相同:

"<Html><Body><p></p> \r\n <p></p>\r\n \r\n \r\n </Body></Html>"

现在它只是清理/删除\r\n

最佳答案

这应该可以解决问题:

XElement root = XmlElement.Load(reader, LoadOptions.PreserveWhitespace);

查看这篇文章了解更多信息:Preserving White Space while Loading or Parsing XML

此外,调用 XElement.ToString() 方法将导致 XElement 应用其自己的格式。为了防止这种情况,您必须调用 XElement.ToString(SaveOptions.DisableFormatting)

关于c# - 在 XElement.Load 上保留\r\n,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6413035/

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