gpt4 book ai didi

c# - 尝试加载到字符串时出现一些奇怪的 XML 问题

转载 作者:行者123 更新时间:2023-12-04 01:16:38 26 4
gpt4 key购买 nike

基本上长话短说,我正在从我的 xml 文件到 c# 丢失一个空间

xml 文件由 Libre Open Office 生成(它是一个带有文本的屏幕),这里是在 Libre Open Office 中显示的文本

your family's monthly

完美,这里是 Notepad++ 显示的 XML

<text:span text:style-name="T5">your family&apos;s</text:span><text:span text:style-name="T8"> </text:span><text:span text:style-name="T5">monthly </text:span>

注意到中间标签中有一个 SPACE 了吗?那很好。

现在,当我进入 C# 时,这里是我的代码...

   XmlDocument doc = new XmlDocument();
doc.Load(@"FILEPATH.xml");
string xmlcontents = doc.InnerXml;

然后当我检查字符串“xmlcontents”的内容时,这就是我所看到的

<text:span text:style-name="T2">your family's</text:span><text:span text:style-name="T3"></text:span><text:span text:style-name="T2">monthly </text:span>

这会输出“your family'smonthly”(不好!)

请注意“family's”和“monthly”之间的空格是怎么消失的?这把我搞砸了,但如果我在加载后编辑 xml 以手动在其中放置一个空格(如在记事本++ 中看到的那样),那很好。

为什么会这样?

最佳答案

这是由 PreserveWhitespace-Option 引起的默认设置为 false。

The Load method always preserves significant white space. The PreserveWhitespace property determines whether or not insignificant white space, that is white space in element content, is preserved. The default is false; white space in element content is not preserved.

这应该有帮助:

XmlDocument doc = new XmlDocument();
doc.PreserveWhitespace = true;
doc.Load(@"FILEPATH.xml");
string xmlcontents = doc.InnerXml;

关于c# - 尝试加载到字符串时出现一些奇怪的 XML 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63253775/

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