By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,524 Members | 1,330 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,524 IT Pros & Developers. It's quick & easy.

How to write XML to a string

P: n/a
I have successfully been using XMLTextWriter to write XML to a file.

But how to I write XML to a string which I can then use to store in a
database?
Tosch
Nov 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi,
One option it to use the StringWriter as the base for your XmlTextWriter
(use the XmlTextWriter contructor that takes a TextWriter as its parameter).
After your are done with all your XML writing, simply call the ToString() on
the StringWriter to get the string representation of the XML.

"Tosch" <to**********@swissonline.ch> wrote in message
news:oo********************************@4ax.com...
I have successfully been using XMLTextWriter to write XML to a file.

But how to I write XML to a string which I can then use to store in a
database?
Tosch
Nov 20 '05 #2

P: n/a
Tosh,

In addition to Shiva, when it is a dataset it can easily with this, (The
same as Shiva said with a XML document, however even shorter)

Serialize
\\\\
Dim sw As New System.IO.StringWriter
ds.WriteXml(sw)
Dim mystring As String = sw.tostring
///
Deserialize
\\\
Dim sr As New System.IO.StringReader(mystring)
Dim ds2 As New DataSet
ds2.ReadXml(sr)
///
I hope this helps a little bit?

Cor
But how to I write XML to a string which I can then use to store in a
database?
Tosch

Nov 20 '05 #3

P: n/a
Tosch,
As the others have suggested, you can use a StringWriter, however be careful
as the StringWriter will assume UTF-16.

Dim buffer As New System.IO.StringWriter
Dim writer As New XmlTextWriter(buffer)

If you want or need the string stored in a different encoding (UTF-8) then
you may want to consider a MemoryStream instead... Or create a class that
inherits from StringWriterwhich overrides the StringWriter.Encoding property
and return the Encoding of your choice.

Hope this helps
Jay

"Tosch" <to**********@swissonline.ch> wrote in message
news:oo********************************@4ax.com...
I have successfully been using XMLTextWriter to write XML to a file.

But how to I write XML to a string which I can then use to store in a
database?
Tosch

Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.