Can't figure out this XmlTextReader exception.

  • Thread starter Thread starter Terry Olsen
  • Start date Start date
T

Terry Olsen

I use the following code to create an XML string:

Private Function CreatePacket(ByVal pt As PacketType) As String
Dim xmlDoc As TextWriter = New StringWriter
Dim xmlWriter As New XmlTextWriter(xmlDoc)
With xmlWriter
..IndentChar = " "
..Indentation = 4
..Formatting = Formatting.Indented
..WriteStartDocument()
..WriteStartElement("BoycoTChatPacket")
..WriteStartElement(pt.ToString)
..WriteAttributeString("UID", "BoycoTChatServer")
..WriteAttributeString("IP", "192.168.0.3")
..WriteAttributeString("Port", "9999")
..WriteAttributeString("Nick", "BoycoTChatServer")
..WriteEndElement() 'pt.ToString
..WriteEndElement() 'BoycoTChatPacket
..WriteEndDocument()
..Flush()
..Close()
End With
xmlDoc.Close()
Return xmlDoc.ToString
End Function

It creates this XML string:

<?xml version="1.0" encoding="utf-16"?>
<BoycoTChatPacket>
<Pong UID="BoycoTChatServer" IP="192.168.0.3" Port="9999"
Nick="BoycoTChatServer" />
</BoycoTChatPacket>

I use the following code to read the XML:

'xmlString contains the XML shown above
Dim xmlReader As New XmlTextReader(xmlString)

And it throws an exception..."Invalid characters in path"

Can anyone see what's wrong here? I'm stumped.

Thanks.
 
Nevermind, I found my mistake. The correct code to read the xmlString is
thus:

Dim sr As New StringReader(rcvd)
Dim xmlReader As New XmlTextReader(sr)
 
Back
Top