Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

help - Convert XmlDocument to string

356 views
Skip to first unread message

SpaceMarine

unread,
Nov 28, 2007, 4:24:39 PM11/28/07
to
hello,

what is the best way to convert an XmlDocument object to a string of
its XML content?

it appears there are a few ways, and im curious what the best one
would be.


thanks,
sm

SpaceMarine

unread,
Nov 28, 2007, 4:50:38 PM11/28/07
to

i found this way, which uses XmlDocument.WriteTo(XmlWriter):

StringWriter sw = new StringWriter();
XmlTextWriter xtw = new XmlTextWriter(sw);

xmlDoc.WriteTo(xtw);
string temp = sw.ToString();

...seems pretty minimal.


sm

Martin Honnen

unread,
Nov 29, 2007, 7:51:41 AM11/29/07
to
SpaceMarine wrote:

You would need to define criteria on what you regard as a good way and
what as a bad way. The shortest in terms of code is certainly
xmlDocumentInstance.OuterXml


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

SpaceMarine

unread,
Nov 30, 2007, 11:34:14 AM11/30/07
to
On Nov 29, 6:51 am, Martin Honnen <mahotr...@yahoo.de> wrote:

> You would need to define criteria on what you regard as a good way and
> what as a bad way. The shortest in terms of code is certainly
> xmlDocumentInstance.OuterXml

i was under the impression that doesnt preserve the original text
entirely -- something about the encoding character-set...ring any
bells?

what are other pros & cons that come to mind? my desire is to preserve
any possible character in its original form.


thanks,
sm

Martin Honnen

unread,
Nov 30, 2007, 11:54:16 AM11/30/07
to

I am not sure I understand. A string in the .NET framework is a sequence
of Unicode characters while the original document might be a file on the
disk which is a sequence of bytes. So a string being a sequence of
characters is always different from a file being a sequence of bytes.

SpaceMarine

unread,
Nov 30, 2007, 12:49:33 PM11/30/07
to
On Nov 30, 10:54 am, Martin Honnen <mahotr...@yahoo.de> wrote:
> > i was under the impression that doesnt preserve the original text
> > entirely -- something about the encoding character-set...ring any
> > bells?
>
> > what are other pros & cons that come to mind? my desire is to preserve
> > any possible character in its original form.
>
> I am not sure I understand. A string in the .NET framework is a sequence
> of Unicode characters while the original document might be a file on the
> disk which is a sequence of bytes. So a string being a sequence of
> characters is always different from a file being a sequence of bytes.

im not sure either, just what i read.

you mentioned other possible "best" ways for doing this, based on
criteria. what sort of options/criteria were you referring to?


thanks,
sm

SpaceMarine

unread,
Nov 30, 2007, 12:58:15 PM11/30/07
to
On Nov 30, 10:54 am, Martin Honnen <mahotr...@yahoo.de> wrote:
> > i was under the impression that doesnt preserve the original text
> > entirely -- something about the encoding character-set...ring any
> > bells?
>
> I am not sure I understand. A string in the .NET framework is a sequence
> of Unicode characters while the original document might be a file on the
> disk which is a sequence of bytes. So a string being a sequence of
> characters is always different from a file being a sequence of bytes.

found the thing i read:

http://www.dotnetmonster.com/Uwe/Forum.aspx/asp-net-caching/225/XmlDocument-and-Session

...a fellow has an XML doc that references UTF-8 encoding. the replier
says:

"Outer XML will give you the XML in UTF-16, in your case it will
strip the encoding attribute. This is because characters in .Net are
16-bit Unicode values."

...my desire is to preserve the original XmlDocument's content w/o
stripping any attributes (even encoding), since i dont know what XML
this code will come across in the future. so i thought that .OuterXml
may not be the best choice. what do you think?


sm

SpaceMarine

unread,
Dec 18, 2007, 2:48:56 PM12/18/07
to

martin, not sure if can still see the entire thread, but its here:

http://groups.google.com/group/microsoft.public.dotnet.xml/browse_thread/thread/da3b4785a42db699

...was curious on your thoughts regarding my last post (on
why .OuterXml may not be ideal).


thanks,
sm

0 new messages