문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top