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
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
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/
> 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
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
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
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