當我在某些場合中對一個字符串(即一個xml文件)進行urlEncode時,它會在文件末尾添加%00字符。我想知道爲什麼會發生這種情況,如果可以防止(我總是可以擦除%00字符)。 xml文件是使用xmlwriter創建的。奇怪的是我使用相同的代碼來創建其他XML文件,編碼後它不會添加%00個字符。UrlEncode .net在字符串末尾添加%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從何而來?
請出示變量'XML的聲明'和創建其內容的代碼。 – Codo 2012-07-26 15:04:38