开发者

XML Safe URLEncoding?

开发者 https://www.devze.com 2022-12-21 16:08 出处:网络
How do I encode an URL that will be \"xml-safe\" where the ampersand(&) in the querystring will not corrupt the XML?

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:

& -> &
' -> '
" -> "
< -> &lt;
> -> &gt;

(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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号