I have some XML returned as a string from a web service (unfortunately I have no control over how it is returned to me. It's usually valid XML, but on occasion I'll receive some that is slightly invalid, which leads to this issue).
The string basically reads like so:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<STATUS _Description="...will contact you with a ("Quote") when ..." />
When I try to do: XDocument.Parse(xmlString);
It throws the following error:
'Quote' is an unexpected token. Expecting white space. Line 15, position 113.
This is to be expected, but I can't figure out the correct string manipulation to fix it. I've tried a number of things including:
static string RemoveInvalidXmlChars(string xmlString)
{
var validXmlChars = xmlString.Where(ch => XmlConvert.IsXmlChar(ch)).ToArray();
return new string(validXmlChars);
}
And: xmlString = xmlString.Replace("\"", """);
(as well as numerous other combinations like (Replace(@"""", "")
, etc.)
Which throws the error:
"'&' is an unexpected token. The expected token is '\"' or '''. Line 1, position 15."}
And I've also tried xmlString = SecurityElement.Escape(xmlString);
(it throws the same error as above). I've also tried using XmlWriter/Reader to modify the string, but the reader throws an error when it reaches the offending element.
My next guess was to use Regular Expressions to convert just the nested quotes to single quotes, but RegEx is kind of foreign to me. How can I fix this so that I can parse it using XDocument.Parse
?