serializing xml - help?

J

Julie

Hi everyone,
I have a method that serializes an object to xml. I was wondering if there
is a way to stop the namespaces from appearing? (these:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema")


Public Shared Function Serialize(Of T)(ByVal options As T) As String
Dim lsResult As String = String.Empty
Dim loSerializer As Xml.Serialization.XmlSerializer = Nothing
Dim loStringWriter As System.IO.StringWriter = Nothing
Dim loXmlWriter As Xml.XmlTextWriter = Nothing

If options Is Nothing Then Return lsResult

Try
loSerializer = New
Xml.Serialization.XmlSerializer(options.GetType)
loStringWriter = New System.IO.StringWriter()

loXmlWriter = New Xml.XmlTextWriter(loStringWriter)
loXmlWriter.Formatting = Xml.Formatting.None

loSerializer.Serialize(loXmlWriter, options)

lsResult = loStringWriter.ToString
Catch ex As Exception
Throw ex
Finally
If loStringWriter IsNot Nothing Then loStringWriter.Close()
End Try

Return lsResult
End Function
 
C

Chris

Hi everyone,
I have a method that serializes an object to xml. I was wondering if there
is a way to stop the namespaces from appearing? (these:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema")

Public Shared Function Serialize(Of T)(ByVal options As T) As String
Dim lsResult As String = String.Empty
Dim loSerializer As Xml.Serialization.XmlSerializer = Nothing
Dim loStringWriter As System.IO.StringWriter = Nothing
Dim loXmlWriter As Xml.XmlTextWriter = Nothing

If options Is Nothing Then Return lsResult

Try
loSerializer = New
Xml.Serialization.XmlSerializer(options.GetType)
loStringWriter = New System.IO.StringWriter()

loXmlWriter = New Xml.XmlTextWriter(loStringWriter)
loXmlWriter.Formatting = Xml.Formatting.None

loSerializer.Serialize(loXmlWriter, options)

lsResult = loStringWriter.ToString
Catch ex As Exception
Throw ex
Finally
If loStringWriter IsNot Nothing Then loStringWriter.Close()
End Try

Return lsResult
End Function

Add the following lines before you call the Serialize method:

Dim xNs As New XmlSerializerNamespaces()
xNs.Add("", "")

Then when you serialize, pass the xNs object to the Serialize method.

Chris
 

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

Top