469,920 Members | 2,479 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Serializing object to XmlDocument (or string)

Tom
I am on .NET 2.0 and am trying to serialize an object to an XmlDocument
(or a string which I can then create an XmlDocument from).

What I have so far is (User is my class);

XmlSerializer x = new XmlSerializer(typeof(User));
MemoryStream memStream = new MemoryStream();

How can I get this to a string or an XmlDocument?

The serialization works fine as if I serialize to a text file (using
StreamWriter), everything looks fine.

I've try converting to bytes, reading on XmlWriter, but nothing seems
to work.

Thanks,

Nov 15 '06 #1
1 9339
Tom
Ok, I figured it out about 5 mins after posting so I thought I would
post my results.

The problem was that after writing to the stream, the stream pointer
was at the end of the stream so when XmlDocument.Load() tried loading
it, it returned "Root Element Missing".

I just moved the pointer back like this:

memStream.Seek(0, System.IO.SeekOrigin.Begin);

Once I did this, XmlDocument.Load() worked fine.

Tom wrote:
I am on .NET 2.0 and am trying to serialize an object to an XmlDocument
(or a string which I can then create an XmlDocument from).

What I have so far is (User is my class);

XmlSerializer x = new XmlSerializer(typeof(User));
MemoryStream memStream = new MemoryStream();

How can I get this to a string or an XmlDocument?

The serialization works fine as if I serialize to a text file (using
StreamWriter), everything looks fine.

I've try converting to bytes, reading on XmlWriter, but nothing seems
to work.

Thanks,
Nov 15 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Nathan | last post: by
5 posts views Thread by AC | last post: by
3 posts views Thread by XenofeX | last post: by
1 post views Thread by bryja_klaudiusz[at]poczta[dot]fm | last post: by
4 posts views Thread by Chumley Walrus | last post: by
9 posts views Thread by Andy Sutorius | last post: by
7 posts views Thread by c676228 | last post: by
3 posts views Thread by Wallace | last post: by
reply views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.