Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / July 2007

Tip: Looking for answers? Try searching our database.

Writing from an XmlReader

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Marc Gravell - 06 Jul 2007 11:55 GMT
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
Martin Honnen - 06 Jul 2007 12:42 GMT
> 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"
[quoted text clipped - 11 lines]
> 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.

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

Marc Gravell - 06 Jul 2007 13:28 GMT
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
Nicholas Paldino [.NET/C# MVP] - 06 Jul 2007 16:08 GMT
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.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> 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
Marc Gravell - 07 Jul 2007 22:37 GMT
> 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
Nicholas Paldino [.NET/C# MVP] - 09 Jul 2007 20:30 GMT
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.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

>> If you want to write it as a stream, then why are you putting it into an
>> XmlReader in the first place?
[quoted text clipped - 10 lines]
>
> Marc
Marc Gravell - 10 Jul 2007 05:02 GMT
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
Marc Gravell - 10 Jul 2007 11:31 GMT
(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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.