How to write XML to a string

  • Thread starter Thread starter Tosch
  • Start date Start date
T

Tosch

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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

XML LINQ - output to XMLtextwriter - the headaches... 2
Edit XML File 1
XML Help 7
Convert Access data to XML 1
XML 2
How to read a Stream into an XElement 0
xml woes 1
Problem about xml 1

Back
Top