Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top