XML Safe URLEncoding?
How do I encode an URL that will be "xml-safe" where the ampersand(&) in the querystring will not corrupt the XML?
For Instance:
www.somedomain.开发者_开发问答com/SomePage.aspx?SomeParam=SomeValue&SomeParam2=SomeVal
The short answer is to encode the URL just like any other text in the document. You won't go wrong if you replace characters as follows:
& -> &
' -> '
" -> "
< -> <
> -> >
(and anybody reading the document with a conforming parser will get the correct, i.e. unencoded, text back).
However, I would echo Anthony's comment: why do you think you need to worry about this? Any sensible XML writing system should take the text you want to represent and do the encoding itself (just like a parser will undo the encoding). Your application should only have to deal with the unencoded text and it should rely on an XML writing library to deal with character encoding and preserving well-formedness. Otherwise you risk being called a bozo. It looks like XmlTextWriter is the .net API that makes all the necessary changes.
The AntiXSS library has an XmlEncode method which you could use.
精彩评论