LINQ to XML: Ignoring of the case of attributes
Asked Answered
D

1

6

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?

Desiccant answered 23/11, 2012 at 8:38 Comment(0)
P
7

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.

Pignus answered 23/11, 2012 at 9:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.