0

I would like to export(convert) XmlTextWriter output data to string variable.

My code as follows :

        '' write data as xml
        Dim objX As New XmlTextWriter(Response.OutputStream, Encoding.UTF8)
        objX.WriteStartDocument()
        objX.WriteStartElement("Transaction")
        objX.WriteAttributeString("version", "1.0")
        objX.WriteElementString("id", "12")
        objX.WriteElementString("token", "4534gfdgdfhfst")


        objX.WriteEndElement()

        objX.WriteEndDocument()

        objX.Flush()

        objX.Close()
        Response.End()

the xml output am getting now is like this : 38824e4760a1b72f9bd95723a3bdffbd02280010.50en3475990rapids1 month rapidshare0.46587748175.136.184.1539/14/2012d7a1ff2200f9fe7b8b89d12fdc8be8f36293‌​712eS. how to make it as xml tags

Dan
  • 343
  • 3
  • 9
  • 21
  • BTW, you shouldn't use `XmlTextWriter`, not since .NET 2.0. Use `XmlWriter.Create` like in Darin's example. – John Saunders Sep 23 '12 at 23:56
  • the xml output am getting now is like this : ?38824e4760a1b72f9bd95723a3bdffbd02280010.50en3475990rapids1 month rapidshare0.46587748175.136.184.1539/14/2012d7a1ff2200f9fe7b8b89d12fdc8be8f36293712eS. how to make it as xml tags – Dan Oct 06 '12 at 14:52

1 Answers1

5

You have configured the XmlTextWriter to write directly to the Response.OutputStream. If you wanted to export it to a string variable you could use a StringWriter that will write to a StringBuilder.

Dim sb As New StringBuilder()
Using sw As New StringWriter(sb)
    Using writer = XmlWriter.Create(sw)
        ' write the XML to the writer here
    End Using
End Using
' At this stage the StringBuilder will contain the generated XML.

as an alternative you could write to a MemoryStream:

Using stream As New MemoryStream()
    Using writer = XmlWriter.Create(stream)
        ' write the XML to the writer here

        Dim xml As String = Encoding.UTF8.GetString(stream.ToArray())
        ' TODO: do something with the generated XML
    End Using
End Using
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Can you write the full code, i tried to export using StringWriter but failed. – Dan Sep 23 '12 at 15:23
  • I have tried the first code and couldnt get the XML format, instead i got data only without tags. – Dan Sep 23 '12 at 16:33
  • Dim sb As New StringBuilder() Using sWriter As New StringWriter(sb) Using xWriter = XmlWriter.Create(sWriter) xWriter.WriteStartDocument() xWriter.WriteStartElement("Transaction") xWriter.WriteAttributeString("version", "1.0") xWriter.WriteElementString("id", "12") xWriter.WriteElementString("token", "4534gfdgdfhfst") xWriter.WriteEndElement() xWriter.WriteEndDocument() xWriter.Flush() xWriter.Close() Response.End() End Using End Using – Dan Sep 23 '12 at 16:34
  • am getting the string as :?38824e4760a1b72f9bd95723a3bdffbd02280010.50en3475990rapids1 month rapidshare0.465 – Dan Sep 23 '12 at 16:40
  • What's wrong with the current code? What is the expected output? What is the actual output? Please don't use the comments section to post code snippets and XML data. Go ahead and update your original question to provide this information. Your comments are completely unredable. – Darin Dimitrov Sep 23 '12 at 16:46
  • Dear Darin, I updated the xml output result and the expected output – Dan Oct 06 '12 at 14:15