Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to XML: Ignoring of the case of attributes

I use LINQ to XML for working with PackageContents.xml file, created by Autodesk company. Company has defined the structure of this XML file here, but without XSD schema.

I have a problem: This company breaks his the rules... Often the case of elements and attributes is invalid. For example, instead of the "URL" attribute the "Url" attribute can be defined (different casing).

But case is important for XML (or, in English):

Like element names, attribute names are case-sensitive and must start with a letter or underscore.

The "URL" is not equals "Url" - such attribute will not found.

Can I solve this problem through LINQ to XML (or other), or I can't?

like image 660
Andrey Bushman Avatar asked Nov 23 '12 08:11

Andrey Bushman


1 Answers

While it's correct that myXelement.Attribute("URL") would require the correct case, there's nothing stopping you doing

var attribute = x.Attributes()
                 .SingleOrDefault(xa => 
                     string.Equals(xa.Name.LocalName, 
                                   "Url", 
                                   StringComparison.InvariantCultureIgnoreCase));

Wrap this up in a suitable extension or static helper method and your consuming code shouldn't be too bad.

like image 69
AakashM Avatar answered Oct 14 '22 02:10

AakashM