質問

I am trying to output a RegEx in an xml file as an Attribute.

The problem is that the RegEx generated in the XML output is different than the one i have in Database:

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

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; has become &amp;apos;.

There are two ways of escaping special characters, you can turn & into &amp; as done here, or you can wrap it in 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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top