tags:

views:

35

answers:

3

I'm trying to set up a situation where a regular expression can occur in xml

I know that the xml parser will complain on several key values (namely, < and &) and from what i understand, attributes can not support CDATA tags.

however since in .net the lookbehind uses the < character, things become more difficult.

What's the best way to handle this? I've considered formatting these as the html equivalent first and decoding them when they're being used. seems like it would work, but was hoping for something more clever.

Is there any more clever ways out there besides formatting as html and then decoding?

+1  A: 

Why not just put them in elements? Either properly encode them, or else put them in CDATA tags. If you use any of the .NET XML APIs and not string manipulation, then .NET will do the encoding for you.

John Saunders
I heart CDATA Tags for this. Keeps the xml legible.
fauxtrot
A: 

"Formatting them as HTML" isn't what you'd be doing. You'd be quoting the characters as XML requires:

<something regex=".*&lt;&amp;whatever">...</something>

When you parse this with an XML parser, the regex attribute will come back as ".*<&whatever", you don't need to do any decoding. The XML parser knows how to handle these entities.

Of course, it might be a challenge to mix regex and XML and have a readable result anyway...

Ned Batchelder
A: 

The System.Web.HttpUtility.HtmlAttributeEncode() function will do the trick for you, if using elements or CDATA is not an option.

kbrimington