XmlSerializer.Serialize Method (XmlWriter, Object, XmlSerializerNamespaces)

Serializes the specified Object and writes the XML document to a file using the specified XmlWriter and references the specified namespaces.

Namespace: System.Xml.Serialization
Assembly: System.Xml (in system.xml.dll)

Public Sub Serialize ( _
	xmlWriter As XmlWriter, _
	o As Object, _
	namespaces As XmlSerializerNamespaces _
Dim instance As XmlSerializer
Dim xmlWriter As XmlWriter
Dim o As Object
Dim namespaces As XmlSerializerNamespaces

instance.Serialize(xmlWriter, o, namespaces)
public void Serialize (
	XmlWriter xmlWriter, 
	Object o, 
	XmlSerializerNamespaces namespaces
public function Serialize (
	xmlWriter : XmlWriter, 
	o : Object, 
	namespaces : XmlSerializerNamespaces



The XmlWriter used to write the XML document.


The Object to serialize.


The XmlSerializerNamespaces referenced by the object.

Exception typeCondition


An error occurred during serialization. The original exception is available using the InnerException property.

When the Serialize method is invoked, the public fields and read/write properties of an object are converted into XML. Methods, indexers, private fields, and read-only properties are not serialized. To serialize all fields and properties, both public and private, use the BinaryFormatter.

Use the xmlWriter parameter to specify an object that derives from the abstract XmlWriter class, which is designed to write XML documents. The XmlTextWriter derives from the XmlWriter.

Note   The XmlSerializer cannot serialize the following: arrays of ArrayList and arrays of List.

The following example serializes an object with an XmlWriter. The example also creates an XmlSerializerNamespaces and adds two namespaces to the object. Several instances of the XmlElementAttribute class are applied to the class members to specify the namespace for each element.

Imports System
Imports System.IO
Imports System.Text
Imports System.Xml
Imports System.Xml.Serialization

' This is the class that will be serialized.
Public Class OrderedItem
    <XmlElement(Namespace := "http://www.cpandl.com")> _
    Public ItemName As String
    <XmlElement(Namespace := "http://www.cpandl.com")> _
    Public Description As String
    <XmlElement(Namespace := "http://www.cohowinery.com")> _
    Public UnitPrice As Decimal
    <XmlElement(Namespace := "http://www.cpandl.com")> _
    Public Quantity As Integer
    <XmlElement(Namespace := "http://www.cohowinery.com")> _
    Public LineTotal As Decimal
    'A custom method used to calculate price per item.
    Public Sub Calculate()
        LineTotal = UnitPrice * Quantity
    End Sub
End Class

Public Class Test
    Public Shared Sub Main()
        Dim t As New Test()
        ' Write a purchase order.
    End Sub    
    Private Sub SerializeObject(ByVal filename As String)
        Console.WriteLine("Writing With XmlTextWriter")
        Dim serializer As New XmlSerializer(GetType(OrderedItem))
        Dim i As New OrderedItem()
        With i
            .ItemName = "Widget"
            .Description = "Regular Widget"
            .Quantity = 10
            .UnitPrice = CDec(2.3)
        End With
        ' Create an XmlSerializerNamespaces object.
        Dim ns As New XmlSerializerNamespaces()
        ' Add two namespaces with prefixes.
        ns.Add("inventory", "http://www.cpandl.com")
        ns.Add("money", "http://www.cohowinery.com")
        ' Create an XmlTextWriter using a FileStream.
        Dim fs As New FileStream(filename, FileMode.Create)
        Dim writer As New XmlTextWriter(fs, New UTF8Encoding())
        ' Serialize using the XmlTextWriter.
        serializer.Serialize(writer, i, ns)
    End Sub
End Class

import System.*;
import System.IO.*;
import System.Text.*;
import System.Xml.*;
import System.Xml.Serialization.*;

// This is the class that will be serialized.
public class OrderedItem
    /** @attribute XmlElement(Namespace = "http://www.cpandl.com")
    public String itemName;
    /** @attribute XmlElement(Namespace = "http://www.cpandl.com")
    public String description;
    /** @attribute XmlElement(Namespace = "http://www.cohowinery.com")
    public System.Decimal unitPrice;
    /** @attribute XmlElement(Namespace = "http://www.cpandl.com")
    public int quantity;
    /** @attribute XmlElement(Namespace = "http://www.cohowinery.com")
    public System.Decimal lineTotal;

    // A custom method used to calculate price per item.
    public void Calculate()
        lineTotal = Decimal.Multiply(unitPrice, Convert.ToDecimal(quantity));
    } //Calculate
} //OrderedItem

public class Test
    public static void main(String[] args)
        Test t = new Test();
        // Write a purchase order.
    } //main

    private void SerializeObject(String fileName)
        Console.WriteLine("Writing With XmlTextWriter");
        XmlSerializer serializer =
            new XmlSerializer(OrderedItem.class.ToType());
        OrderedItem i = new OrderedItem();
        i.itemName = "Widget";
        i.description = "Regular Widget";
        i.quantity = 10;
        i.unitPrice = Convert.ToDecimal(2.3);

        // Create an XmlSerializerNamespaces object.
        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();

        // Add two namespaces with prefixes.
        ns.Add("inventory", "http://www.cpandl.com");
        ns.Add("money", "http://www.cohowinery.com");

        // Create an XmlTextWriter using a FileStream.
        Stream fs = new FileStream(fileName, FileMode.Create);
        XmlWriter writer = new XmlTextWriter(fs, new UTF8Encoding());

        // Serialize using the XmlTextWriter.
        serializer.Serialize(writer, i, ns);
    } //SerializeObject
} //Test

<?xml version="1.0"?>
 <OrderedItem xmlns:inventory="http://www.cpandl.com" xmlns:money="http://www.cohowinery.com">
   <inventory:Description>Regular Widget</inventory:Description>

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

Community Additions