How to remove whitespace from an XmlDocument

Asked
Viewd27280

6

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#.

27280

2 ответов

19

Установите для флага preserveWhitespace значение false:

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

To remove white spaces between the tags:

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

Source and other usefull info here