CPK_2011
CPK_2011

Reputation: 1150

Convert xml to pure string

I am trying to serialize the class object to string. But it is giving output in the form of xml from the below code.

Dim x As New Xml.Serialization.XmlSerializer(response.GetType)
Dim sw As New IO.StringWriter()
x.Serialize(sw, response)
Return sw.ToString

Current Output

<Employees>
<Employee>John</Employee>
<Employee>Peter</Employee>
</Employees>

Expected Output

<Employees><Employee>John</Employee><Employee>Peter</Employee></Employees>

Upvotes: 1

Views: 8537

Answers (2)

Steven Doggart
Steven Doggart

Reputation: 43743

You need to use an XmlTextWriter so that you can specify how you want the XML output formatted. For instance:

Dim x As New Xml.Serialization.XmlSerializer(response.GetType)
Using sw As New IO.StringWriter()
  Using xw As New XmlTextWriter(sw)
    xw.Formatting = Formatting.None
    x.Serialize(xw, response)
    Return sw.ToString
  End Using
End Using 

Upvotes: 6

Sandy Gifford
Sandy Gifford

Reputation: 8136

Don't convert the response text into an XML object, keep it as a string.

From there this post should be enlightening. Once that's all said and done and stored in a variable, you can do all the XML serializing your heart desires.

Upvotes: 1

Related Questions