XmlNode Innertext 中的回车 [英] Carriage return in XmlNode Innertext
问题描述
我正在使用 System.Xml 来解析我生成的 xml 文件.节点的一些内部文本包含这样的回车:\r\n[...]\r\n".那是因为我在解析它之前使用了 Visual Studio 对其进行了格式化.
I'm using System.Xml to parse a xml file I generated. Some of the inner text of the nodes contains carriage return like this: " \r\n[...]\r\n ". Thant is because I used Visual Studio to format it before I parse it.
有什么办法可以去掉格式化工具添加的回车符?
Is there any way that remove the carriage return added by formating tools?
谢谢!
编辑@Jon:真的谢谢!我用类似的方法来规避这个问题.
Edit @Jon: Really thanks! I use a similar way to circumvent the problem.
char[] escape = { ' ', '\r', '\n' };
string text = node.InnerText.Trim(escape);
这样可以避免过滤掉文本中的回车符.
This avoid filtering out carriage returns in the text.
正如您所提到的,我真正的问题是如何在解析 xml 时过滤掉格式.我的假设是:节点中只有纯文本,没有嵌入子元素.
As you mentioned, my real question is how to filter out formatings while parsing xml. My assumption is: there is just plain text in the node, no child element embeded.
除了回车还有其他格式吗?
Is there any other formattings except for carriage returns?
推荐答案
删除所有换行符很容易:
node.InnerText = node.InnerText.Replace("\r", "")
.Replace("\n", "");
但是您的问题是专门删除格式化工具添加的换行符 - 如果它们都在文件中,您就无法真正检测到哪些是手动添加的,哪些是自动添加的.
But your question asks about specifically removing newlines added by formatting tools - and you can't really detect which ones have been added manually and which automatically, if they're all just in the file.
此外,您是否有散布文本和元素的节点,例如:
Also, do you have nodes which have text and elements interspersed, such as:
<node>
text
<element />
text
</node>
如果这样做,您真的需要单独处理每个文本节点,而不仅仅是更改 InnerText
.
If you do, you really need to process each text node individually instead of just changing InnerText
.
如果这对您没有帮助,您能否提供有关该问题的更多信息?
If this isn't helpful to you, could you give more information about the problem?
这篇关于XmlNode Innertext 中的回车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!