C# 如何从 XmlDocument 中删除空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1231374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 13:59:19  来源:igfitidea点击:

How to remove whitespace from an XmlDocument

c#xml

提问by Eros

I have an XML document from which I want to remove white spaces and carriage returns. How can I get the modified XML using C#.

我有一个 XML 文档,我想从中删除空格和回车。如何使用 C# 获取修改后的 XML。

采纳答案by Oliver Turner

Set the preserveWhitespace flag to false:

将preserveWhitespace 标志设置为false:

XmlDocument doc = new XmlDocument();
doc.PreserveWhitespace = false;
doc.Load("foo.xml");
// doc.InnerXml contains no spaces or returns

回答by code-ninja

To remove white spaces between the tags:

要删除标签之间的空格:

# Regex regex = new Regex(@">\s*<");  
# string cleanedXml = regex.Replace(dirtyXml, "><");

Source and other usefull info here

来源和其他有用的信息在这里