在格式错误的xml中转义属性中的内部引号

时间:2021-03-22 22:29:44

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

我有以下第三方xml包含格式错误的XML片段,我正在尝试使用XmlTextReader来读取它。

   <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:

XmlTextReader抛出异常:

'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?

它挂在了引号中:[“Upload_Service”]。关于如何逃避这些引用的任何想法,以便XmlTextReader可以解析数据?

1 个解决方案

#1


0  

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

您可以尝试使用更具容错能力的html解析器(如HtmlAgilityPack)来解析xml。

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);

#1


0  

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

您可以尝试使用更具容错能力的html解析器(如HtmlAgilityPack)来解析xml。

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);