当我在一些ocassions中对一个字符串(即一个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来自哪里?
答案 0 :(得分:4)
MemoryStream.GetBuffer
上的评论提供了适当的指导:
请注意,缓冲区包含可能未使用的已分配字节。例如,如果将字符串“test”写入MemoryStream对象,则从GetBuffer返回的缓冲区长度为256而不是4,未使用252个字节。要仅获取缓冲区中的数据,请使用ToArray方法;但是,ToArray会在内存中创建数据副本。
修改你的代码:
Dim xml As String = Encoding.GetEncoding("iso-8859-1").GetString(ms.ToArray)
事实上,在这种情况下更好的选择是使用StringBuilder
:
Dim sb As New StringBuilder
Using writer As XmlWriter = XmlWriter.Create(sb, xmlWriterSettings)
' ...
End Using
Dim xml as String = sb.ToString()
答案 1 :(得分:1)
我相信ms.GetBuffer
包含的内容比您想象的要多。 %00
表示NULL,我的猜测是缓冲区末尾包含填充NULL。
取而代之的是:
Using ms As New IO.MemoryStream
Dim 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
ms.Position = 0
Dim xml As String = ms.ReadToEnd()
Dim XmlEncoded As String = HttpUtility.UrlEncode(xml)
End Using
有关从MemoryStream
获取字符串的详细信息,请参阅this question。
请参见this documentation,详细说明缓冲区包含可能未使用的已分配字节。