0

I use an XMLWriter to manually make an XML document. Is there a way to put that in string form so I can write it to my DB?

I am coding in VB.Net

MPelletier
  • 16,256
  • 15
  • 86
  • 137
Sean P
  • 949
  • 4
  • 22
  • 41
  • The code you have so far would be a good start. Also - what database system do you use? – Tomalak Feb 16 '10 at 17:18
  • http://stackoverflow.com/questions/955611/xmlwriter-to-write-to-a-string-instead-of-to-a-file – womp Feb 16 '10 at 17:20

2 Answers2

5

You can make it write to a StringBuilder:

StringBuilder sb = new StringBuilder():
using (var writer = XmlWriter.Create(sb))
{
    // write the xml
}

string writtenXml = sb.ToString();

In VB.NET:

Dim sb As New StringBuilder()

Using writer As XmlWriter = XmlWriter.Create(sb)
    ' write the xml '
End Using

Dim writtenXml As String = sb.ToString()
Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343
0

You have have your XmlWriter wrap a StringWriter. Then you process it as you already are. when you want to access the string itself call StringWriter.ToString().

Ian Jacobs
  • 5,456
  • 1
  • 23
  • 38