Wil*_*ies 1 c# xml xml-parsing
我已经阅读了 % 字符的类似帖子,但似乎其他问题可以在标题行中解决。XML 中是否不允许使用某些字符,或者我是否需要以另一种方式格式化文档(在我的情况下,“=”字符在尝试在 C# 中读取文档时给我带来了麻烦)?
名称不能以字符 ' ' 开头,也类似但仍由标题固定。
XElement nodes = XElement.Load(filename);
Run Code Online (Sandbox Code Playgroud)
XML 的结构如下:
<?xml version="1.0" encoding="utf-8"?>
<offer>
<data id="Salary">
<ocrstring>which is equal to $60,000.00 if working 40 hours per week</ocrstring>
<rule>.*(([+-]?\$[0-9]{1,3}(?:,?[0-9]{3})*\.[0-9]{2}))</rule>
<output></output>
</data>
<data id="Hours">
<ocrstring></ocrstring>
<rule>"(?<=working).*?(?=hours)"</rule> <!-- Error Occurring Here -->
<output>bob</output>
</data>
<data id="Location">
<ocrstring></ocrstring>
<rule>Regex2</rule>
<output>LongWindingRoad222</output>
</data>
</offer>
Run Code Online (Sandbox Code Playgroud)
如何解析 XML 文档而不会出现无法以字符开头的“=”错误
您需要对所有<rule>元素使用 CDATA 部分。
XML
<?xml version="1.0" encoding="utf-8"?>
<offer>
<data id="Salary">
<ocrstring>which is equal to $60,000.00 if working 40 hours per week</ocrstring>
<rule><![CDATA[.*(([+-]?\$[0-9]{1,3}(?:,?[0-9]{3})*\.[0-9]{2}))]]></rule>
<output></output>
</data>
<data id="Hours">
<ocrstring></ocrstring>
<rule><![CDATA["(?<=working).*?(?=hours)"]]></rule>
<!-- Error Occurring Here -->
<output>bob</output>
</data>
<data id="Location">
<ocrstring></ocrstring>
<rule>Regex2</rule>
<output>LongWindingRoad222</output>
</data>
</offer>
Run Code Online (Sandbox Code Playgroud)