C#: Lower Case All XML Tags with Regex

ByMel

C#: Lower Case All XML Tags with Regex

Sometimes when accepting an XML document from an uncontrolled source using Linq to XML, it’s useful to convert all tags and attributes to lower case before processing the XML. This is because Linq to XML is case-sensitive and you can’t always rely on the program producing the XML to follow your casing standard for elements and attributes.

So here’s a quick and dirty single line of code that will accomplish just this in C# using a regular expression:

Regex.Replace(
    xml, 
    @"<[^<>]+>",
    m => { return m.Value.ToLower(); }, 
    RegexOptions.Multiline | RegexOptions.Singleline);

And here’s that functionality all nice and wrapped up inside of an extension for XElement:

public static class XElementExt
{
    public static string LowerCaseTags(string xml)
    {
        return Regex.Replace(
            xml,
            @"<[^<>]+>",
            m => { return m.Value.ToLower(); },
            RegexOptions.Multiline | RegexOptions.Singleline);
    }
}

Note: The Regex class is defined in System.Text.RegularExpressions

Here’s an example of the resulting affect.

Before:

<ELEMENT>
    <ChildTag>
        <inner>This text Will not Be Harmed!</inner>
    </ChildTag>
</ELEMENT>

After:

<element>
    <childtag>
        <inner>This text Will not Be Harmed!</inner>
    </childtag>
</element>

You’ll notice that with this method all text within element tags is converted to lower case. This means that attribute values will lose any special casing they may have had, which may or may not be a problem for what you’re doing.

About the author

Mel administrator

13 Comments so far

Anand KishorePosted on5:03 am - Dec 17, 2018

i need to ignore anchor tag href links because there is a case-sensitive in that link how to write can you please suggest.

TrafzPosted on7:35 am - Jan 22, 2013

This ruins CDATA 🙁

sliper dragonPosted on1:35 pm - Nov 8, 2012

thanks…

Thiti LeelitthamPosted on1:33 am - Jun 14, 2012

Very use full. Thx 😉

KhawarPosted on6:33 am - Apr 20, 2010

Great work…..
problem solved… =))

raveePosted on1:48 am - Apr 8, 2010

tahnks a toneee gr8 work

DänuPosted on2:49 pm - Dec 2, 2009

Thanks mate, used this one to lower case mshtml generated html tags.

KaanPosted on4:41 am - Jul 4, 2009

I and i aren’t same in Turkish..
So, Isparta isn’t isparta..
İstanbul = istanbul
Isparta = ısparta
<meta name="keywords" content="Isparta, İstanbul…

    MelPosted on3:41 pm - Jul 4, 2009

    Hey Kaan,

    Thanks for the comment! Yes, if you’re implementing this method you need to be sure that any case-sensitive data is not stored in attributes, but is instead stored inside the element.

    Cheers!

SenthilPosted on6:24 am - May 6, 2009

how to read website link in the XML file? using c# .net

Leave a Reply to Khawar Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.