Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

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)

'Declaration
Public Sub Serialize ( _
	xmlWriter As XmlWriter, _
	o As Object, _
	namespaces As XmlSerializerNamespaces _
)
'Usage
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
)

Parameters

xmlWriter

The XmlWriter used to write the XML document.

o

The Object to serialize.

namespaces

The XmlSerializerNamespaces referenced by the object.

Exception typeCondition

InvalidOperationException

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.
        t.SerializeObject("simple.xml")
    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)
            .Calculate()
        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)
        writer.Close()
    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.
        t.SerializeObject("simple.xml");
    } //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);
        i.Calculate();

        // 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);
        writer.Close();
    } //SerializeObject
} //Test

<?xml version="1.0"?>
 <OrderedItem xmlns:inventory="http://www.cpandl.com" xmlns:money="http://www.cohowinery.com">
   <inventory:ItemName>Widget</inventory:ItemName>
   <inventory:Description>Regular Widget</inventory:Description>
   <money:UnitPrice>2.3</money:UnitPrice>
   <inventory:Quantity>10</inventory:Quantity>
   <money:LineTotal>23</money:LineTotal>
 </OrderedItem>


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
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.