Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove whitespace from an XmlDocument

Tags:

c#

xml

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

like image 228
Eros Avatar asked Aug 05 '09 05:08

Eros


People also ask

What is whitespace in XML?

White space is used in XML for readability and has no business meaning. Input XML messages can include line breaks, blanks lines, and spaces between tags (all shown in the following example). If you process XML messages that contain any of these spaces, they are represented as elements in the message tree.

What is XmlDocument?

The XmlDocument class is an in-memory representation of an XML document. It implements the W3C XML Document Object Model (DOM) Level 1 Core and the Core DOM Level 2. DOM stands for document object model. To read more about it, see XML Document Object Model (DOM).


3 Answers

Set the preserveWhitespace flag to false:

XmlDocument doc = new XmlDocument();
doc.PreserveWhitespace = false;
doc.Load("foo.xml");
// doc.InnerXml contains no spaces or returns
like image 67
Oliver Turner Avatar answered Nov 11 '22 08:11

Oliver Turner


To remove white spaces between the tags:

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

Source and other usefull info here

like image 35
code-ninja Avatar answered Nov 11 '22 08:11

code-ninja


I solved this just using a more complete regex:

var regex = new Regex(@"[\s]+(?![^><]*(?:>|<\/))");
var cleanedXml = regex.Replace(xml, "");

This regex will remove all the spaces between closed tags.

Input example:

<root>
   <result success="1"/>
   <userID>12345</userID>
   <classID>  56543  </classID>
</root>

Output for the input:

<root><result success="1"/><userID>12345</userID><classID>  56543  </classID>
</root>

A more complete explanation of this regex can be found on this post: https://stackoverflow.com/a/25771445/6846888

like image 1
Ângelo Polotto Avatar answered Nov 11 '22 07:11

Ângelo Polotto