Groups | Blog | Home
all groups > dotnet xml > july 2003 >

dotnet xml : NetworkStream/CryptoStream/XMLTextReader Problems in VB.net



Casey Watson
7/10/2003 4:51:12 PM
Hi :)

I'm having some major trouble with an XML Client/Server application that I
am writing. I am using NetworkStream with CryptoStream to Read and Write
XML between computers. Now, whenever I write the XML, the Server section
which uses the XMLTextReader Class will not accept EndElements period, and
neither the ReadStartElement or ReadEndElement will work properly. However,
if I only send to WriteStartElements over the network, the Server will
receive it fine if I only use the Read Method. It will not accept
EndElements period. Since this application is just a test application, I'm
running it all on the same computer multi-threaded. If this is the problem,
please tell me so. I don't think it is, however, becuase if I just send
XMLStartElements, it reads those just fine with the XML.Read method no
matter how many start elements I send. I'm sure there's a very easy fix to
this, but, just in case, I have attached some of the more pertinent code
segments for your review. I didn't include the Listener section and the
Connect section because both of those work fine. Any help that anybody could
provide would be greatly appreciated. Thank you for your time.

Server App::

Button3.Text = "Waiting..."

Button3.ForeColor = Color.DarkRed

Dim ConnectedStream As Net.Sockets.NetworkStream

ConnectedStream = Server_Client.GetStream

Dim XMLDoc As New Xml.XmlDocument()

Dim CryptoReadStream As New Security.Cryptography.CryptoStream _

(ConnectedStream, _

EncProvider.CreateDecryptor(EncProvider.Key, EncProvider.IV), _

Security.Cryptography.CryptoStreamMode.Read)

Dim XMLRead As New Xml.XmlTextReader(CryptoReadStream)

XMLRead.WhitespaceHandling = Xml.WhitespaceHandling.None

Do While 1

XMLRead.Read()

Loop



Client App::

Dim ConnectedStream As Net.Sockets.NetworkStream

ConnectedStream = Client_Client.GetStream

Dim CryptoWriteStream As New Security.Cryptography.CryptoStream _

(ConnectedStream, _

EncProvider.CreateEncryptor(EncProvider.Key, EncProvider.IV), _

Security.Cryptography.CryptoStreamMode.Write)

Dim XMLWrite As New Xml.XmlTextWriter(CryptoWriteStream,
System.Text.Encoding.Default)

Button4.Text = "Sending..."

Button4.ForeColor = Color.DarkRed

XMLWrite.WriteStartElement("Start1")

XMLWrite.WriteEndElement()

XMLWrite.WriteStartElement("Start2")

XMLWrite.WriteEndElement()

XMLWrite.Flush()

XMLWrite.Close()

Button4.Text = "Message Sent!"

Button4.ForeColor = Color.DarkGreen


Thank you so much!!!
If you can help me out at all, I would surely appreciate it.

-- Casey J. Watson



Sergey
7/13/2003 8:31:58 AM
Not sure that this is exactly the problem, and the only problem.
In you example you are sending not well-formed document but document
fragment:
<Start1><Start1/>
<Start2><Start2/>

To change this you can wrap your fragment into additional element:
XMLWrite.WriteStartElement("root")
XMLWrite.WriteStartElement("Start1")
XMLWrite.WriteEndElement()
XMLWrite.WriteStartElement("Start2")
XMLWrite.WriteEndElement()
XMLWrite.WriteEndElement()

Sergey

[quoted text, click to view]

AddThis Social Bookmark Button