473,321 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

Various XML streaming problems

I'm trying to process a MemoryStream of XML data, which at any point in
time may have incomplete fragments (because data arrives from a
socket).

If I process data until XmlTextReader.Read() returns false, fills it
with more data, and reverses the position of the underlying
MemoryStream, the XmlTextReader immediately stops working. I apparently
have to re-create the XmlTextReader instance when I make changes to the
MemoryStream.

The XmlTextReader constructor apparently advances the position of the
MemoryStream to the end even before I've tried to Read(), which makes
it hard to know exactly where in the underlying stream Read() attempts
fail.

So the bottom line is, I want to be able to successively append
incoming data from a socket and parse it as XML, without missing
anything. Is there a way to do it? I've looked like crazy on the web
and in relevant newsgroups without finding a solution.

I'm posting sample code which demonstrates my problems. I'd really
appreciate any ideas at all on how to make it work!

The output from my sample code is:

--- First test, which works ---
Read data: SomeElement
Read data: SomeElement
--- Second test, throws an exception ---
Read data: SomeElement
Read data: NestedElement
XmlException!
--- Third test, fails ---
XmlException!
Read data:

My sample code (sorry for the somewhat poor formatting):

// XmlProblem.cs

using System;
using System.IO;
using System.Xml;
using System.Text;
using System.Diagnostics;

namespace XmlTest
{
class Test
{
void Run()
{
XmlProcessor r;

Debug.WriteLine("--- First test, which works ---");
// In this test my data consists of a legal start and end
element.
r = new XmlProcessor();
r.Insert("<SomeElement></SomeElement>");
r.Parse();

Debug.WriteLine("--- Second test, throws an exception ---");
// In this test I have a nested element and neither has
// corresponding end elements, because these haven't yet been
// read from our imaginary (in this sample) socket. This
// works ok but I'd really like to get rid of the exception,
// because MSDN states that the state of an XmlTextReader
// after an XmlException is unreliable so I'd have to throw
// it away and reinitialize it anyway, making it difficult to
// add more data.
r = new XmlProcessor();
r.Insert("<SomeElement><NestedElement>");
r.Parse();

Debug.WriteLine("--- Third test, fails ---");
// In this test our imaginary socket retrieves fragments of
// the element in pieces of two. Unfortunately even the first
// attempt to read throws an exception and the second read
// returns incomplete data.
r = new XmlProcessor();
r.Insert("<Some");
r.Parse();
r.Insert("Element>");
r.Parse();

Debug.Flush();
}

[STAThread]
static void Main(string[] args)
{
Test t = new Test();
t.Run();
}
}

public class XmlProcessor
{
private MemoryStream m_Stream = new MemoryStream();
private XmlTextReader m_Reader = null;

public XmlProcessor()
{

}

public void Insert(string data)
{
byte[] buffer = Encoding.ASCII.GetBytes(data.ToString());
long beforePos = m_Stream.Position;
m_Stream.Write(buffer, 0, buffer.Length);
m_Stream.Position = beforePos;
m_Reader = new XmlTextReader(m_Stream, XmlNodeType.Element,
null);
}

public bool Parse()
{
try
{
while (m_Reader.Read())
{
Debug.WriteLine("Read data: " + m_Reader.Name);
}
}
catch (XmlException)
{
Debug.WriteLine("XmlException!");
}
return false;
}
}
}

Nov 12 '05 #1
0 1455

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Tom John | last post by:
Hi I am using Office Web Components to generate a GIF image, which i want to display in a picture box. However i am having a problem when i attempt to load the image into the picture box....
8
by: poorna | last post by:
hi all i want to upload the video files to the server.. then i encode all the video files into flv files ... and then i am go to streaming ... in the mean while i create the thumbnail image...
8
by: Tony K | last post by:
Is streaming audio capable in asp.net 2.0 / AJAX? What I mean by streaming audio is: ability to play one or more songs back to back...or maybe even let the user select several songs to play and it...
1
by: Faisal Shafiq | last post by:
I want to upload a file direct to the Silverlight Streaming Service from a Web Client such as silverlight application. As per our product requirement we want to upload a .WMV file directly from...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.