473,850 Members | 2,067 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XmlReader to XmlTextWriter without XmlDocument

Im currently using the following code..

XmlDocument xmlData = new XmlDocument();
XmlTextWriter xmlwriter = new
XmlTextWriter(R esponse.OutputS tream,System.Te xt.Encoding.UTF 8);
XmlReader xmlreader = SqlHelper.Execu teXmlReader(... )
xmlData.Load(xm lreader);
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 2493
Hi,

I think you could move to the root node by calling
XmlReader.MoveT oContent, then XmlReader.ReadO uterXml 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.Write Node(xmlreader, true);

any reason why this would be an isssue?

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

However, because XmlWriter.Write Node 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.WriteNod e(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.Write Node 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.WriteNod e(reader, false);
}
}


Or just

while (!reader.EOF){
writer.WriteNod e(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.WriteNod e(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.WriteNod e(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.WriteNod e(reader, false)
is enough. If Read() method was called, loop is necessary to write the
whole XML:
while (!reader.EOF)
writer.WriteNod e(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.co m> wrote in message news:uc******** ******@TK2MSFTN GP10.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.Initi al 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.End Element
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
1343
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 = passParameters (xmlMenu.SelectSingleNode("document/setup")); // just a local function to get all parameters in the doc XslTransform xslTransform = new XslTransform();
5
6172
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 insert in the front. My code is give below. Is it possible to do without using XMLDOcument? Dim masterDoc As String = Request.PhysicalApplicationPath & "PageViews.xml" Dim writer As XmlTextWriter = Nothing
7
8377
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
11544
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 having issues using XmlTextWriter, saving it out to a file with UTF8 encoding, and seeing "dirty", or "human unreadable" characters show up *right before* the XML declaration. I need to have the XML declaration state "encoding = utf-8", but also...
3
15808
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 anyway. All I want is moving a finished document from XmlTextWriter into an XmlDocument. Can anyone give a short example? Gustaf
1
2436
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 final Excel sheet. What's added to the InnerText of an XmlDocument is something similar to:
8
10482
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 on closing my stream, and I can't convince it not to. A much-simplified example is below; basically, as soon as the writer is disposed (marked **** below) the base stream gets closed - which is a pain if I was still using it ;-p The base...
2
7609
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 ( moForm == null )
2
5605
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 bytes into utf-8. MemoryStream ms = new MemoryStream(); XmlTextWriter xmlWriter = new XmlTextWriter(ms, new UTF8Encoding(false)); writer.Formatting = Formatting.Indented;
0
9741
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10724
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10351
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9503
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7899
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7071
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5929
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4546
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4140
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.