我有以下第三方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抛出异常:
'Upload_Service' is an unexpected token. Expecting white space. Line 44126, position 123.
它挂在引号中:[“Upload_Service”]。关于如何逃避这些引用的任何想法,以便XmlTextReader可以解析数据?
答案 0 :(得分:0)
您可以尝试使用更具容错能力的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);