HighTechTalks DotNet Forums  

help - Convert XmlDocument to string

Dotnet XML microsoft.public.dotnet.xml


Discuss help - Convert XmlDocument to string in the Dotnet XML forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
SpaceMarine
 
Posts: n/a

Default help - Convert XmlDocument to string - 11-28-2007 , 03:24 PM






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

Reply With Quote
  #2  
Old   
SpaceMarine
 
Posts: n/a

Default Re: help - Convert XmlDocument to string - 11-28-2007 , 03:50 PM






On Nov 28, 3:24 pm, SpaceMarine <spacemar... (AT) mailinator (DOT) com> wrote:
Quote:
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.
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


Reply With Quote
  #3  
Old   
Martin Honnen
 
Posts: n/a

Default Re: help - Convert XmlDocument to string - 11-29-2007 , 06:51 AM



SpaceMarine wrote:

Quote:
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.
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/


Reply With Quote
  #4  
Old   
SpaceMarine
 
Posts: n/a

Default Re: help - Convert XmlDocument to string - 11-30-2007 , 10:34 AM



On Nov 29, 6:51 am, Martin Honnen <mahotr... (AT) yahoo (DOT) de> wrote:

Quote:
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


Reply With Quote
  #5  
Old   
Martin Honnen
 
Posts: n/a

Default Re: help - Convert XmlDocument to string - 11-30-2007 , 10:54 AM



SpaceMarine wrote:
Quote:
On Nov 29, 6:51 am, Martin Honnen <mahotr... (AT) yahoo (DOT) 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.
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.


--

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


Reply With Quote
  #6  
Old   
SpaceMarine
 
Posts: n/a

Default Re: help - Convert XmlDocument to string - 11-30-2007 , 11:49 AM



On Nov 30, 10:54 am, Martin Honnen <mahotr... (AT) yahoo (DOT) de> wrote:
Quote:
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


Reply With Quote
  #7  
Old   
SpaceMarine
 
Posts: n/a

Default Re: help - Convert XmlDocument to string - 11-30-2007 , 11:58 AM



On Nov 30, 10:54 am, Martin Honnen <mahotr... (AT) yahoo (DOT) de> wrote:
Quote:
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/For...nt-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


Reply With Quote
  #8  
Old   
SpaceMarine
 
Posts: n/a

Default Re: help - Convert XmlDocument to string - 12-18-2007 , 01:48 PM



On Nov 30, 10:54 am, Martin Honnen <mahotr... (AT) yahoo (DOT) de> wrote:
Quote:
SpaceMarine wrote:
On Nov 29, 6:51 am, Martin Honnen <mahotr... (AT) yahoo (DOT) 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.

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.
martin, not sure if can still see the entire thread, but its here:

http://groups.google.com/group/micro...3b4785a42db699

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


thanks,
sm


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.