简体   繁体   English

RegEx应该以CDATA或属性的形式存储在XML中吗?

[英]Should RegEx be stored in XML as CDATA or as Attributes?

I am trying to output a RegEx in an xml file as an Attribute. 我试图在XML文件中将RegEx作为属性输出。

The problem is that the RegEx generated in the XML output is different than the one i have in Database: 问题在于XML输出中生成的RegEx与我在数据库中生成的RegEx不同:

-- database
[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@([a-z0-9_-]+\.)+(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|[a-z]{2})

-- generated
[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@([a-z0-9_-]+\.)+(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|[a-z]{2})

Should i save the RegEx as a <![CDATA[ regex ]]> or is this just the default behavior of xml files (encoding some characters and decode them automatically when i read it back) ? 我应该将RegEx保存为<![CDATA[ regex ]]>还是这只是xml文件的默认行为(对某些字符进行编码,并在我读回时自动对其进行解码)?

This is the code i have to generate the element: 这是我必须生成元素的代码:

XElement attr =
    new XElement("Attribute",
        new XAttribute("RegEx", item.RegularExpression)
    );

And this is what it generates: 这就是它产生的:

<Attribute RegEx="[a-z0-9!#$%&amp;amp;&amp;apos;*+\/=?^_`{|}~-]+(\.[a-z0-9!#$%&amp;amp;&amp;apos;*+\/=?^_`{|}~-]+)*@([a-z0-9_-]+\.)+(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|[a-z]{2})" />

The problem is that the regex in the database already uses XML escaping, and when copying it into the XML file, you have added another layer of escaping, so &apos; 问题在于数据库中的正则表达式已经在使用XML转义,并且在将其复制到XML文件时,您已经添加了另一层转义,因此&apos; has become &amp;apos; 已成为&amp;apos; .

There are two ways of escaping special characters, you can turn & into &amp; 有两种转义特殊字符的方法,您可以将&变成&amp; as done here, or you can wrap it in CDATA. 就像这里所做的一样,也可以将其包装在CDATA中。 In this case you don't want to do either, because it is already escaped. 在这种情况下,您既不想执行任何操作,因为它已经被转义。

I'm not familiar with link-to-xml so I don't know how to do this correctly in that environment. 我不熟悉link-to-xml,所以我不知道如何在该环境中正确执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM