XML安全URL编码?安全、XML、URL

2023-09-07 00:20:32 作者:百年大妓,毁于一蛋

我如何连接code,这将是XML安全的网址,其中的符号(安培)?在查询字符串不会破坏XML

How do I encode an URL that will be "xml-safe" where the ampersand(&) in the querystring will not corrupt the XML?

有关实例:

www.somedomain.com/SomePage.aspx?SomeParam=SomeValue&SomeParam2=SomeVal

www.somedomain.com/SomePage.aspx?SomeParam=SomeValue&SomeParam2=SomeVal

推荐答案

简短的回答是EN code就像文档中的任何其他文字的网址。你会不会出问题,如果你替换字符如下:

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;

(和任何人读取该文件与符合解析器会得到正确的,即unen codeD,背课文)。

(and anybody reading the document with a conforming parser will get the correct, i.e. unencoded, text back).

不过,我要重申安东尼的评论:为什么你认为你需要担心这一点?任何明智的XML写作系统应该把你要重新present并执行编码本身(就像一个解析器将撤消编码)的文本。您的应用程序应该只处理unen codeD文本,它应该依赖于XML编写的库来处理字符编码和preserving良好性。否则,你的风险被称为笨蛋。它看起来像的XmlTextWriter 是.NET API使所有必要的更改。

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.