The default XML serialization adds the xml header and a couple of namespace definitions – elements which aren’t always welcome. Here’s how to control the XML source.
Let’s start with a simple class called ‘Book’:
Public Class Book
Public Title As String = String.Empty
Public Sub New()
End Sub
Public Sub New(ByVal title As String)
Me.Title = title
End Sub
End Class
Our mini-project (in this case: a console application) should be able to create a new instance of the book class and serialize it:
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization
Module Main
Sub Main()
Dim book As New Book("XML Serialization")
Dim result As String
Dim serializer As New XmlSerializer(GetType(Book))
Using writer As New StringWriter()
serializer.Serialize(writer, book)
result = writer.ToString
End Using
Console.WriteLine(result)
Console.ReadLine()
End Sub
End Module
The result looks like this:
<?xml version="1.0" encoding="utf-16"?>
<Book
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Title>XML Serialization</Title>
</Book>
Task #1: removing the namespace definitions
In order to remove the namespace definitions, we need to define a new set of namespaces and tell our serializer to use this set. We can do this by using the XmlSerializerNamespaces class. We need to add the following lines just after the XmlSerializer definition:
Dim nameSpaces As XmlSerializerNamespaces = _
New XmlSerializerNamespaces()
nameSpaces.Add("", "")
and we also need to replace line 15 in the original code with the following statement:
serializer.Serialize(writer, book, nameSpaces)
This way our XML source becomes:
<?xml version="1.0" encoding="utf-16"?> <Book> <Title>XML Serialization</Title> </Book>
Task #2: Removing the XML header
Next thing on the to-do list is to find a way to produce a plain XML file without any definitions whatsoever. To accomplish this, we need to further modify our code and use the XmlWriterSettings class with OmitXmlDeclaration property set to true. Replace lines 14-18 with the following:
Dim settings As New XmlWriterSettings()
settings.OmitXmlDeclaration = True
settings.Indent = True
Using writer As New StringWriter()
Using xmlWriter As XmlWriter = _
xmlWriter.Create(writer, settings)
serializer.Serialize(xmlWriter, book, nameSpaces)
result = writer.ToString
End Using
End Using
Our XML now becomes:
<Book> <Title>XML Serialization</Title> </Book>
Task #3: adding our own namespace definition
Our final task is to insert our own namespace definition (and only that one). First thing to do is to instantiate the XmlSerializer with our own namespace. Then we need to add our namespace to our custom namespace list. Replace the serializer definition with:
Dim serializer As New XmlSerializer(GetType(Book), _
"urn:our_namespace")
add change the nameSpaces.add statement with:
nameSpaces.Add("", "urn:our_namespace")
The last set of changes produce the following XML:
<Book xmlns="urn:our_namespace"> <Title>XML Serialization</Title> </Book>
There you have it.
