Removing White Space from an XML Fragment

I recently encountered the need to remove extraneous white space from a string containing valid XML. Well, it probably wasn't absolutely necessary but I was tired of seeing a ton of "\r\n" and tabs in the values displayed in the Visual Studio debugger.

In any case, a quick search of the web uncovered a variety of approaches, most centering on setting the XmlDocument property preserveWhiteSpace to false. These didn't work for me since I wasn't dealing with an XML document but, rather, a string or fragment that contained XML.

However, I found on CodeProject.com an elegant solution using regular expressions:

xmlText = Regex.Replace(
            xmlText, @"(?=<[^=]+?>)(?=</?\w+\s+\w+)(<.*?)(\s+)(.*?>)",@"$1_$3",
            RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);

where xmlText is the string which contains your XML fragment.

Comments

Popular posts from this blog

Using Reference Aliases

List of Visual Studio Keyboard Shortcuts