Chris
Chris

Reputation: 1203

Escaping inner quotes in attributes in malformed xml

I have the following third party xml containing the malformed XML snippet and I am trying to use XmlTextReader to read through it.

   <ReplacementDefinitions>
        <Def key="EnclosingFunction.name" value="_jspService(System.Configuration.ConfigurationManager.ConnectionStrings["Upload_Service"].ToString())"/>
        <Def key="PrimaryCall.name" value="print()"/>
        <Def key="PrimaryLocation.file" value="Error.jsp"/>
        <Def key="PrimaryLocation.line" value="20"/>
        <LocationDef path="webapp/jsp/common/Error.jsp" line="20" lineEnd="20" colStart="27" colEnd="0" key="PrimaryLocation"/>
      </ReplacementDefinitions>

XmlTextReader is throwing the exception:

'Upload_Service' is an unexpected token. Expecting white space. Line 44126, position 123.

Its getting hung up on the quotes in: ["Upload_Service"]. Any ideas on how I can escape these quotes so that XmlTextReader can parse the data?

Upvotes: 3

Views: 465

Answers (1)

L.B
L.B

Reputation: 116138

You can try to parse your xml with html parsers(like HtmlAgilityPack) which are more fault tolerant.

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(xml);

var defs = doc.DocumentNode.Descendants("def")
        .ToDictionary(d=>d.Attributes["key"].Value,d=>d.Attributes["value"].Value);

Upvotes: 1

Related Questions