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

How move from XmlTextWriter to XmlDocument?

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
Nov 12 '05 #1
3 15769
Gustaf Liljegren wrote:
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?


Use XmlNodeWriter (find it on gotdotnet.com).
--
Oleg Tkachenko [XML MVP, MCP]
http://blog.tkachenko.com
Nov 12 '05 #2
Oleg Tkachenko [MVP] wrote:
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?
Use XmlNodeWriter (find it on gotdotnet.com).


I couldn't find how. The only documentation I found was this example:

XmlDocument DocumentFromObject(object obj) {
XmlDocument doc = new XmlDocument();
XmlSerializer s = new XmlSerializer(obj.GetType());
XmlNodeWriter w = new XmlNodeWriter(doc);
s.Serialize(w, c);
return doc;
}

What is 'obj' and 'c'? One of them ought to be the XmlTextWriter in my
case, but what is the other?

I found another way through:

System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.IO.StringWriter sw = new System.IO.StringWriter(sb);
XmlTextWriter w = new XmlTextWriter(sw);
w.WriteStartDocument();
....
w.WriteEndDocument();
w.Flush();
w.Close();
XmlDocument x = new XmlDocument();
x.LoadXml(sb.ToString());

Maybe there's a gotcha here, but I haven't found one so far.

Gustaf
Nov 12 '05 #3
This might not compile but it is close and should get you on the right
track...
System.IO.MemoryStream stream = new System.IO.MemoryStream();
System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(stream,
System.Text.Encoding.UTF8);

// use writer to write XML

// reset the stream position to beginning
stream.Position = 0;

System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(stream);

Cheers,

~Mork
http://blogs.morksoft.com/danmork/

Nov 12 '05 #4

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

Similar topics

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...
2
by: Jesper Stocholm | last post by:
I use an XmlTextWriter to generate xml data. The declaration is: System.Text.StringBuilder sb = new System.Text.StringBuilder(); System.IO.StringWriter sw = new System.IO.StringWriter(sb);...
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...
8
by: Charles.Deisler | last post by:
Im currently using the following code.. XmlDocument xmlData = new XmlDocument(); XmlTextWriter xmlwriter = new XmlTextWriter(Response.OutputStream,System.Text.Encoding.UTF8); XmlReader...
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...
0
by: Martin | last post by:
Hi, I am retriving data from sql server using the xml raw clause and an xmltextwriter. this is working out fine except the xml raw clause does not put a root node on the xml that is returned...
0
by: Martin | last post by:
Hi, I am retriving data from sql server using the xml raw clause and an xmltextwriter. this is working out fine except the xml raw clause does not put a root node on the xml that is returned...
4
by: quest | last post by:
Is there anyway I can generate the xml in the following format using XmlTextWriter ? Intended output: <?xml version="1.0" ?> I tried: XmlTextWriter xmlWriter = new...
2
by: ajc308 | last post by:
My current code contains a hardcoded filename for the XmlTextWriter to write to, and then an XmlDocument loads that file. I am looking to replace the hardcoded filename with something, anything that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.