XElement and XDocument in LINQ raise exception when an attribute value contains a special character

EDN Admin

Staff member
Aug 7, 2010
In the Machine
The below code is throwing xmlException -var appContexts = from suitFile in GetValidSuitFiles(directoryToTraverse)
let contextNode = XElement.Load(suitFile)
where IsAppContextNode(contextNode) &&
contextNode.HasElements &&
select new AppContextInfo(contextNode);

Exception is coming from XElement.Load(fileName). The file contains a special character as part of the value of an attribute not in the attribute name or the tag name.
The exception obtained is -
"<special character is here. I copied it but could not paste it.>, hexadecimal value 0x03, is an invalid character. Line 287, position 103.".
We need to be able to load the file with the special character and we also need to use LINQ as we need to process 100s of files.
Any help is appreciated.

View the full article