Home > OS >  Attempting to serialize a class without a namespace or declaration
Attempting to serialize a class without a namespace or declaration

Time:12-28

I've been following the solution found on How can I make the xmlserializer only serialize plain xml? in an attempt to only serialize plain text however I'm running into a few problems as I do this on the VB.net side

The aim is to prevent the lines <?xml version="1.0" encoding="utf-16"?> and attributes xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" from showing

I have a sub as follows:

  Private Sub writeXMLContent()
        For Each dataItem As dataClass In dataSet            
            Dim emptyNameSpace As XmlSerializerNamespaces = New XmlSerializerNamespaces({XmlQualifiedName.Empty})
            Dim serializer As New XmlSerializer(GetType(dataClass))
            Dim settings As New XmlWriterSettings
            settings.Indent = True
            settings.OmitXmlDeclaration = True

            Using stream As New StringWriter
                Using writer = XmlWriter.Create(stream, settings)
                    serializer.Serialize(writer, serializer, emptyNameSpace)
                    'will write each line to a file here
                End Using
            End Using
        Next
    End Sub

however I keep coming across the same two errors:

  1. The line Using writer = XmlWriter.Create(stream, settings) throws an error using operand of type object must implement system.iDisposable
  2. The line serializer.Serialize(writer, serializer, emptyNameSpace) seems to not like my second parameter as it expects an object? I'm not too sure what object I would pass here?

CodePudding user response:

  1. The XmlWriter does not implement IDisposable, i.e., it has no Dispose method that the Using statement could call. Simply fix it by not using the Using-statement.

    Dim writer = XmlWriter.Create(stream, settings)
    
  2. The second parameter must be the object that you want to serialize, i.e., probably dataItem in this case.

    serializer.Serialize(writer, dataItem)
    

As for removing the namespaces and the comment, here is a solution:

Sub Test()
    Dim dataItem = New DataClass With {.Id = 5, .Name = "Test"}

    ' Serialize.
    Dim serializer As New XmlSerializer(GetType(DataClass))
    Dim sb As New StringBuilder()
    Using writer As New StringWriter(sb)
        serializer.Serialize(writer, dataItem)
    End Using

    Dim xml = RemoveNamespaces(sb.ToString())

    Console.WriteLine(xml)
End Sub

Private Function RemoveNamespaces(ByVal xml As String) As String
    Dim doc = New XmlDocument()
    doc.LoadXml(xml)

    ' This assumes that we have only a namespace attribute on the root element.
    doc.DocumentElement.Attributes.RemoveAll()

    Dim settings As New XmlWriterSettings With {.Indent = True, .OmitXmlDeclaration = True}
    Dim sb As New StringBuilder()
    Using stringWriter As New StringWriter(sb)
        Using writer = XmlWriter.Create(stringWriter, settings)
            doc.WriteTo(writer)
        End Using
    End Using
    Return sb.ToString()
End Function

It is using this test class

Public Class DataClass
    Public Property Id As Integer
    Public Property Name As String
End Class
  • Related