Writing from an XmlReader

M

Marc Gravell

Is there an easy way to (efficiently) walk the entire contents of an
XmlReader? For instance, I have an XmlReader returned from a "FOR XML"
SQL query (ExecuteXmlReader), and I want to return this data to the
caller through a stream.

One option is to use:
XmlDocument doc = new XmlDocument();
doc.Load(reader);
doc.Save(stream);

but obviously this unnecessarily loads the DOM; likewise,
reader.ReadOuterXml() forces the entire xml to load into a string at
once... but the xml in question could be quite large. I was hoping for
a stream-like way to walk the nodes, writing to the stream (either
directly, or via an XmlWriter).

Does anybody have any hints here?

Marc
 
M

Martin Honnen

Marc said:
Is there an easy way to (efficiently) walk the entire contents of an
XmlReader? For instance, I have an XmlReader returned from a "FOR XML"
SQL query (ExecuteXmlReader), and I want to return this data to the
caller through a stream.

One option is to use:
XmlDocument doc = new XmlDocument();
doc.Load(reader);
doc.Save(stream);

but obviously this unnecessarily loads the DOM; likewise,
reader.ReadOuterXml() forces the entire xml to load into a string at
once... but the xml in question could be quite large. I was hoping for
a stream-like way to walk the nodes, writing to the stream (either
directly, or via an XmlWriter).

XmlWriter has a method WriteNode that takes an XmlReader
<http://msdn2.microsoft.com/en-us/library/System.Xml.XmlWriter.WriteNode.aspx>
That might be what you are looking for although I am not sure I have
understood that correctly.
 
M

Marc Gravell

Perfect; from the name, I presumed it would write a single node - but
it does write the subtree. Cheers - I knew it would be there
somewhere...

Marc
 
N

Nicholas Paldino [.NET/C# MVP]

Marc,

If you want to write it as a stream, then why are you putting it into an
XmlReader in the first place? Do you have to do some processing on it
before you send it back? If not, then I would just get it as a string or
byte array and then place that into a MemoryStream.
 
M

Marc Gravell

If you want to write it as a stream, then why are you putting it into an
XmlReader in the first place?

Very simple; since the data might be quite large, I was hoping that
this might allow the ADO provider to stream the data from the
database, rather than loading it in a lump (as would be the case with
a string or byte[]). A SqlDataReader might allow me to read chunks of
binary, but I've only managed to do this with "image" data-types
before, not a FOR XML output. I haven't looked in detail, but a brief
peek with "Reflector" leads me to believe that ExecuteXmlReader will
stream... if you know of a way to get a FOR XML directly streaming as
byte[] chunks, I'd be interested?

Marc
 
N

Nicholas Paldino [.NET/C# MVP]

Marc,

You can do this by passing the SequentialAccess value from the
CommandBehavior enumeration to the ExecuteReader method on your command.
Once you do that, you can call GetBytes or GetChars to access the data for
the field as a progressive stream (you have to access the columns in order,
btw), as opposed to all at once.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Marc Gravell said:
If you want to write it as a stream, then why are you putting it into an
XmlReader in the first place?

Very simple; since the data might be quite large, I was hoping that
this might allow the ADO provider to stream the data from the
database, rather than loading it in a lump (as would be the case with
a string or byte[]). A SqlDataReader might allow me to read chunks of
binary, but I've only managed to do this with "image" data-types
before, not a FOR XML output. I haven't looked in detail, but a brief
peek with "Reflector" leads me to believe that ExecuteXmlReader will
stream... if you know of a way to get a FOR XML directly streaming as
byte[] chunks, I'd be interested?

Marc
 
M

Marc Gravell

Interesting - I'll give that a go. I've used that approach with BLOBs
(and (less often) CLOBs), but I haven't (to date) used it with the xml
data-type. Cheers.

Marc
 
M

Marc Gravell

(for the list's benefit)
Nicholas is quite correct; I've changed my code to use a fixed char[]
buffer, ExecuteReader(), and GetChars(), writing the appropriate
buffer-portion to the Stream via a StreamWriter and the .Write(char[],
int, int) overload. All is now well (and a little quicker I think,
presumably due to ExecuteXmlReader() having to parse the stream).

Thanks.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top