469,271 Members | 1,009 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

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 15220
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Jesper Stocholm | last post: by
8 posts views Thread by Charles.Deisler | last post: by
reply views Thread by Martin | last post: by
reply views Thread by Martin | last post: by
4 posts views Thread by quest | last post: by
2 posts views Thread by ajc308 | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.