serializing xml - help?

  • Thread starter Thread starter Julie
  • Start date Start date
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
 
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
 
Back
Top