473,320 Members | 1,722 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,320 software developers and data experts.

XmlReader to XmlTextWriter without XmlDocument

Im currently using the following code..

XmlDocument xmlData = new XmlDocument();
XmlTextWriter xmlwriter = new
XmlTextWriter(Response.OutputStream,System.Text.En coding.UTF8);
XmlReader xmlreader = SqlHelper.ExecuteXmlReader(...)
xmlData.Load(xmlreader);
xmlData.WriteTo(xmlwriter);
xmlwriter.Flush();

Is there a way to get around using the XmlDocument for this situation?

Thanks in advance!

Nov 12 '05 #1
8 2473
Hi,

I think you could move to the root node by calling
XmlReader.MoveToContent, then XmlReader.ReadOuterXml and write the
returned string to the response.

That only write the root node content, so if you also want to write the
things prior to it (if any), you may need some extra code.

Does it work?

----
Thi - http://thith.blogspot.com

Nov 12 '05 #2
i think i found a solution that removes the xmldocument..

while (xmlreader.Read()) xmlwriter.WriteNode(xmlreader, true);

any reason why this would be an isssue?

Nov 12 '05 #3
Yes, that is cool!

However, because XmlWriter.WriteNode also moves the reader to the start
of the next sibling then you should not call Read() again. Something
like this would work:

if (reader.Read()) // move to the first node
{
while (!reader.EOF){
writer.WriteNode(reader, false);
}
}

I don't think the XmlReader returned from DB would have any default
attributes, so I pass false for the second param.

Nov 12 '05 #4
Truong Hong Thi wrote:
Yes, that is cool!

However, because XmlWriter.WriteNode also moves the reader to the start
of the next sibling then you should not call Read() again. Something
like this would work:

if (reader.Read()) // move to the first node
{
while (!reader.EOF){
writer.WriteNode(reader, false);
}
}


Or just

while (!reader.EOF){
writer.WriteNode(reader, false);
}

Because WriteNode() method can handle reader in initial state, see
http://msdn.microsoft.com/library/de...enodetopic.asp

--
Oleg Tkachenko [XML MVP, MCAD]
http://www.XmlLab.Net | http://www.XLinq.Net | http://blog.tkachenko.com
Nov 12 '05 #5
Only the following line is fine:
writer.WriteNode(reader, false);

According to MSDN as I followed Oleg's link, if the reader is in the
initial state, this method moves the reader to the end of file (not to
the first node).

Because WriteNode() method can handle reader in initial state, see
http://msdn.microsoft.com/library/de...enodetopic.asp

--
Oleg Tkachenko [XML MVP, MCAD]
http://www.XmlLab.Net | http://www.XLinq.Net | http://blog.tkachenko.com


Nov 12 '05 #6
Truong Hong Thi wrote:
Only the following line is fine:
writer.WriteNode(reader, false);


Hmmm, really. I always thought looping until EOF is necessary because
XML can contain comments and PIs before root element, but it seems to be
working without looping just fine too.

So, if a reader is in an initial state, writer.WriteNode(reader, false)
is enough. If Read() method was called, loop is necessary to write the
whole XML:
while (!reader.EOF)
writer.WriteNode(reader, false);

--
Oleg Tkachenko [XML MVP, MCAD]
http://www.XmlLab.Net | http://www.XLinq.Net | http://blog.tkachenko.com
Nov 12 '05 #7
Awesome! Many thanks guys!

Nov 12 '05 #8
"Oleg Tkachenko [MVP]" <so**@body.com> wrote in message news:uc**************@TK2MSFTNGP10.phx.gbl...
Hmmm, really. I always thought looping until EOF is necessary because XML can contain comments and PIs before root element, but it
seems to be working without looping just fine too.


Reason being that ReadState.Initial is tantamount to being at the proverbial
"root" of the document as XPath defines it (where the root is essentially in
outer space as far as the document element is concerned). ReadState.EOF
happens only after the Read( ) that produced the XmlNodeType.EndElement
of the document element. Therefore, it too is conceptually beyond the end
of the document. Taken altogether, all XmlDecls, Comments, PIs, etc.,
that exist outside of the document element node are still contained by
the "root." :-)

Yes, if any Read( ) has already ventured into the document then the loop
is necessary for WriteNode( ).
Derek Harmon
Nov 12 '05 #9

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

Similar topics

0
by: Shivonne | last post by:
Hi all Having problems with the XmlReader object. I'm trying to transform an XML document with XSLT - and get the resulting output. My code is as follows: XsltArgumentList xslArgs =...
5
by: reddy | last post by:
I am trying to insert a node into an XMLFile. using XMLTextwriter. My Question is Is it possible to do without using XMLDocument. Because its loading all the the file into memory. I just want to...
7
by: Leszek | last post by:
Hello, I have an XmlReader object containg some Xml code. I would like to export this document from the reader to an Xml file. How to do this? Thanks for any hints, Leszek Taratuta
4
by: H Lee | last post by:
Hi, I'm an XML newbie, and not sure if this is the appropriate newsgroup to post my question, so feel free to suggest other newgroups where I should post this message if this is the case. I'm...
3
by: Gustaf Liljegren | last post by:
I think I must use MemoryStream, but whenever I search for examples of MemoryStream, I get lots byte-per-byte reading, buffers and so on. It's hard to understand and seem overkill for my case...
1
by: st | last post by:
Hi, I've a routine that exports a DB query to Excel by building an XmlDocument and saving to a XmlTextWriter. I'm having trouble with carriage returns in a mailing address not showing up in the...
8
by: Marc Gravell | last post by:
I want to write a method that will accept a stream as a parameter, and which will write xml to the stream (based in reality on database results) using the XmlTextWriter class. However, this insists...
2
by: raj | last post by:
i have this code which is upgraded to work with 2.0 and works great with framework 1.0 and 1.1 without the upgraded code. //2.0 code// public static XmlReader RtfToXml( string Rtf ) { if (...
2
by: SammyBar | last post by:
Hi all, I'm trying to convert the xml obtained from a XmlReader object into a UTF-8 array. My general idea is to read the XmlReader and write into a MemoryStream. Then convert the MemoryStream...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
1
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...
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
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.