Reputation: 20161
I'm receiving data via an XML API and it's returning a node like the following:
<?xml version='1.0' encoding='utf-8' ?>
<location>
<name>ØL Shop</name>
</location>
I have no control over the response but I am trying to Load it into an XDocument in which it fails due to the invalid character.
Is there anything I can do to make this load properly? I want to keep the solution as general as possible because it is possible other invalid characters exist.
Thoughts?
Upvotes: 2
Views: 480
Reputation: 82096
Why not just escape any invalid XML characters before you load the response into an XDocument
? You could use a regex for this, should be relatively straight forward.
See escape invalid XML characters in C#
Upvotes: 0
Reputation: 8937
You cant use "&" symbol in XDocument.Parse input text. Replace it with "&" , like this
<?xml version='1.0' encoding='utf-8' ?>
<location>
<name>&Oslash;L Shop</name>
</location>
Upvotes: 1
Reputation: 35353
You can use html parsers which are more tolerant to invalid inputs. For example; (using HtmlAgilityPack) this code works without any problem.
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(xml);
var name = doc.DocumentNode.Descendants("name").First().InnerText;
Upvotes: 1