Everything you do with XML is case sensitive, I know that.
However, right now I find myself in a situation, where the software I'm writing would yield much fewer errors if I somehow made xml name/attribute recognition case insensitive. Case insensitive XPath would be a god sent.
Is there an easy way/library to do that in c#?
I use another solution. The reason people want this is because you don't want to duplicate the name of the property in the class file in an attribute as well. So what I do is add a custom attribute to all properties:
This way the XML serializer can map lower case properties to CamelCased classes.
The properties on the classes still have a decorator that says that something is different, but you don't have the overhead of marking every property with a name:
You can create case-insensitive methods (extensions for usability), e.g.:
I would start by converting all tags and attribute names to lowercase, leaving values untouched, by using
SAX
parsing, ie. withXmlTextReader
.An XMl document can have two different elements named respectively:
MyName
andmyName
-- that are intended to be different. Converting/treating them as the same name is an error that can have gross consequences.In case the above is not the case, then here is a more precise solution, using XSLT to process the document into one that only has lowercase element names and lowercase attribute names:
when this transformation is applied on any XML document, for example this one:
the wanted, correct result (element and attribute names converted to lowercase) is produced:
Once the document is converted to your desired form, then you can perform any desired processing on the converted document.
XML is text. Just
ToLower
it before loading to whatever parser you are using.So long as you don't have to validate against a schema and don't mind the values being all lower case, this should work just fine.
The fact is that any XML parser will be case sensitive. If it were not, it wouldn't be an XML parser.