M
Matt Stephens
I am trying to use the XmlTextReader class with a NetworkStream to help
simplify parsing xml nodes. My app listens for a TCP connection on port
6000. I am using HyperTerminal to connect and manually type the xml for
testing purposes, however after 2 or 3 characters the XmlTextReader throws
an XmlException with it's message property saying 'The data at the root
level is invalid. Line 1, position 1.'.
If i substitute the NetworkStream for a FileStream then everything works
fine. I then wrote a MyStream class derived from Stream and in all my
overrides just delegated to a Stream object passed into the constructor and
wrote out to the console what methods the XmlTextReader was calling and what
the parameters and return codes were. The only difference was that the
FileStream objects Read method can read the entire file in one go wheras the
NetworkStream only gets 1 character per read, the bytes returned are exactly
the same up until the XmlException is thrown. Is this a bug with
XmlTextReader or am i just missing something?
The following is the code i am experimenting with:
IPAddress address = Dns.Resolve("localhost").AddressList[0];
TcpListener listener = new TcpListener(address,6000);
listener.Start();
TcpClient client = listener.AcceptTcpClient(); // Blocks until
connection made
NetworkStream netstream = client.GetStream();
XmlTextReader xmlreader = new XmlTextReader(netstream);
try
{
while(xmlreader.EOF==false)
{
if(xmlreader.Read()==true)
{
Console.WriteLine("Line: {0} Pos: {1} Name:
{2}",xmlreader.LineNumber,xmlreader.LinePosition,xmlreader.Name);
}
}
}
catch(XmlException e)
{
Console.WriteLine("woops! Line:{0} Pos:{1}
Msg:{2}",e.LineNumber,e.LinePosition,e.Message);
}
Thanks in advance for any help or ideas anyone can give,
Matt Stephens
simplify parsing xml nodes. My app listens for a TCP connection on port
6000. I am using HyperTerminal to connect and manually type the xml for
testing purposes, however after 2 or 3 characters the XmlTextReader throws
an XmlException with it's message property saying 'The data at the root
level is invalid. Line 1, position 1.'.
If i substitute the NetworkStream for a FileStream then everything works
fine. I then wrote a MyStream class derived from Stream and in all my
overrides just delegated to a Stream object passed into the constructor and
wrote out to the console what methods the XmlTextReader was calling and what
the parameters and return codes were. The only difference was that the
FileStream objects Read method can read the entire file in one go wheras the
NetworkStream only gets 1 character per read, the bytes returned are exactly
the same up until the XmlException is thrown. Is this a bug with
XmlTextReader or am i just missing something?
The following is the code i am experimenting with:
IPAddress address = Dns.Resolve("localhost").AddressList[0];
TcpListener listener = new TcpListener(address,6000);
listener.Start();
TcpClient client = listener.AcceptTcpClient(); // Blocks until
connection made
NetworkStream netstream = client.GetStream();
XmlTextReader xmlreader = new XmlTextReader(netstream);
try
{
while(xmlreader.EOF==false)
{
if(xmlreader.Read()==true)
{
Console.WriteLine("Line: {0} Pos: {1} Name:
{2}",xmlreader.LineNumber,xmlreader.LinePosition,xmlreader.Name);
}
}
}
catch(XmlException e)
{
Console.WriteLine("woops! Line:{0} Pos:{1}
Msg:{2}",e.LineNumber,e.LinePosition,e.Message);
}
Thanks in advance for any help or ideas anyone can give,
Matt Stephens