当我在某些情况下对字符串(即 xml 文件)进行 urlEncode 时,它会在文件末尾添加 %00 字符。我想知道为什么会发生这种情况以及是否可以防止(我总是可以删除 %00 个字符)。xml 文件是使用 xmlwriter 创建的。奇怪的是我使用相同的代码来创建其他 xml 文件,并且在对它们进行编码后它不会添加 %00 个字符。
例子:
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE peticion >
<peticion>
<nombre>Info hotel</nombre>
<agencia>HOTUSA</agencia>
<tipo>15</tipo>
</peticion>
编辑:创建 xml 这就是我所做的。
Dim xmlWriterSettings As New System.Xml.XmlWriterSettings
With xmlWriterSettings
.Encoding = Encoding.GetEncoding("iso-8859-1")
.OmitXmlDeclaration = False
.Indent = True
End With
Dim ms As New IO.MemoryStream
Using writer As System.Xml.XmlWriter = System.Xml.XmlWriter.Create(ms, xmlWriterSettings)
With writer
.WriteDocType("peticion", Nothing, Nothing, Nothing)
.WriteStartElement("peticion")
.WriteElementString("nombre", "Info hotel")
.WriteElementString("agencia", "HOTUSA")
.WriteElementString("tipo", "15")
.WriteEndElement()
End With
End Using
Dim xml As String = Encoding.GetEncoding("iso-8859-1").GetString(ms.GetBuffer)
Dim XmlEncoded As String = HttpUtility.UrlEncode(xml)
XmlEncoded 包含:
%3c%3fxml+version%3d%221.0%22+encoding%3d%22iso-8859-1%22%3f%3e%0d%0a%3c!DOCTYPE+peticion+%3e%0d%
0a%3cpeticion%3e%0d%0a++%3cnombre%3eInfo+hotel%3c%2fnombre%3e%0d%0a++%3cagencia%3eHOTUSA%3c%
2fagencia%3e%0d%0a++%3ctipo%3e15%3c%2ftipo%3e%0d%0a%3c%2fpeticion%3e%00%00%00%00%00%00%00%00%00%
00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%
00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%
00%00%00%00%00%00%00%00%00%00%00%00%00%00
所有这些 %00 是从哪里来的?