XmlSerializer.Serialize 方法

定义

将对象序列化到 XML 文档中。

重载

Serialize(XmlWriter, Object)

使用指定的 Object 序列化指定的 XmlWriter 并将 XML 文档写入文件。

Serialize(XmlWriter, Object, XmlSerializerNamespaces, String, String)

使用指定的 Object、XML 命名空间和编码序列化指定的 XmlWriter 并将 XML 文档写入文件。

Serialize(XmlWriter, Object, XmlSerializerNamespaces, String)

使用指定的 XmlWriter 和指定命名空间及编码样式序列化指定对象并将 XML 文档写入文件。

Serialize(XmlWriter, Object, XmlSerializerNamespaces)

使用指定的 Object 和指定命名空间序列化指定的 XmlWriter 并将 XML 文档写入文件。

Serialize(TextWriter, Object, XmlSerializerNamespaces)

使用指定的 Object 和指定命名空间序列化指定的 TextWriter 并将 XML 文档写入文件。

Serialize(Object, XmlSerializationWriter)

使用指定的 Object 序列化指定的 XmlSerializationWriter 并将 XML 文档写入文件。

Serialize(TextWriter, Object)

使用指定的 Object 序列化指定的 TextWriter 并将 XML 文档写入文件。

Serialize(Stream, Object)

使用指定的 Object 序列化指定的 Stream 并将 XML 文档写入文件。

Serialize(Stream, Object, XmlSerializerNamespaces)

使用引用指定命名空间的指定 Stream 序列化指定的 Object 并将 XML 文档写入文件。

Serialize(XmlWriter, Object)

Source:
XmlSerializer.cs
Source:
XmlSerializer.cs
Source:
XmlSerializer.cs

使用指定的 Object 序列化指定的 XmlWriter 并将 XML 文档写入文件。

C#
public void Serialize (System.Xml.XmlWriter xmlWriter, object o);
C#
public void Serialize (System.Xml.XmlWriter xmlWriter, object? o);

参数

xmlWriter
XmlWriter

用于编写 XML 文档的 XmlWriter

o
Object

要序列化的 Object

例外

序列化期间发生错误。 使用 InnerException 属性时可使用原始异常。

示例

以下示例使用 XmlWriter序列化对象。

C#
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

// This is the class that will be serialized.
public class OrderedItem4
{
    public string ItemName;
    public string Description;
    public decimal UnitPrice;
    public int Quantity;
    public decimal LineTotal;
    // A custom method used to calculate price per item.
    public void Calculate()
    {
        LineTotal = UnitPrice * Quantity;
    }
}

public class Test4
{
    public static void Main()
    {
        Test4 t = new();
        // Write a purchase order.
        t.SerializeObject("simple.xml");
    }

    private void SerializeObject(string filename)
    {
        Console.WriteLine("Writing With XmlTextWriter");

        XmlSerializer serializer = new(typeof(OrderedItem4));
        OrderedItem4 i = new()
        {
            ItemName = "Widget",
            Description = "Regular Widget",
            Quantity = 10,
            UnitPrice = (decimal)2.30
        };
        i.Calculate();
        // Create an XmlTextWriter using a FileStream.
        Stream fs = new FileStream(filename, FileMode.Create);
        XmlTextWriter writer = new(fs, Encoding.Unicode);
        // Serialize using the XmlTextWriter.
        serializer.Serialize(writer, i);
        writer.Close();
    }
}
XML
<?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>

注解

方法 Serialize 将对象的公共字段和读/写属性转换为 XML。 它不转换方法、索引器、私有字段或只读属性。

在 参数中 xmlWriter ,指定派生自抽象 XmlWriter 类的对象。 XmlTextWriter派生自 XmlWriter

备注

XmlSerializer无法序列化以下内容: 的ArrayList数组和 的List<T>数组。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

Serialize(XmlWriter, Object, XmlSerializerNamespaces, String, String)

Source:
XmlSerializer.cs
Source:
XmlSerializer.cs
Source:
XmlSerializer.cs

使用指定的 Object、XML 命名空间和编码序列化指定的 XmlWriter 并将 XML 文档写入文件。

C#
public void Serialize (System.Xml.XmlWriter xmlWriter, object? o, System.Xml.Serialization.XmlSerializerNamespaces? namespaces, string? encodingStyle, string? id);
C#
public void Serialize (System.Xml.XmlWriter xmlWriter, object o, System.Xml.Serialization.XmlSerializerNamespaces namespaces, string encodingStyle, string id);

参数

xmlWriter
XmlWriter

用于编写 XML 文档的 XmlWriter

o
Object

要序列化的对象。

namespaces
XmlSerializerNamespaces

XmlSerializerNamespaces 的实例,其中包含要使用的命名空间和前缀。

encodingStyle
String

该文档中使用的编码。

id
String

对于 SOAP 编码消息,这是用于生成 id 特性的基。

注解

id 参数提供用于创建 SOAP ID 的基字符串。 默认情况下,它们是“id1”、“id2”等。 但如果参数设置为“myBase”,则生成的值为“myBaseid1”、“myBaseid2”等。 此功能用于在整个 SOAP 消息中创建唯一的 ID 值。

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Serialize(XmlWriter, Object, XmlSerializerNamespaces, String)

Source:
XmlSerializer.cs
Source:
XmlSerializer.cs
Source:
XmlSerializer.cs

使用指定的 XmlWriter 和指定命名空间及编码样式序列化指定对象并将 XML 文档写入文件。

C#
public void Serialize (System.Xml.XmlWriter xmlWriter, object? o, System.Xml.Serialization.XmlSerializerNamespaces? namespaces, string? encodingStyle);
C#
public void Serialize (System.Xml.XmlWriter xmlWriter, object o, System.Xml.Serialization.XmlSerializerNamespaces namespaces, string encodingStyle);

参数

xmlWriter
XmlWriter

用于编写 XML 文档的 XmlWriter

o
Object

要序列化的对象。

namespaces
XmlSerializerNamespaces

该对象所引用的 XmlSerializerNamespaces

encodingStyle
String

序列化的 XML 的编码样式。

例外

序列化期间发生错误。 使用 InnerException 属性时可使用原始异常。

注解

Serialize调用 方法时,对象的公共字段和读/写属性将转换为 XML。 不序列化方法、索引器、私有字段和只读属性。

xmlWriter使用 参数指定派生自抽象XmlWriter类的对象,该类旨在编写 XML 文档。 XmlTextWriter派生自 XmlWriter

encodingStyle 参数设置为“http://schemas.xmlsoap.org/soap/encoding/"对于 SOAP 版本 1.1 编码;否则,请将其设置为“http://www.w3.org/2001/12/soap-encoding"用于 SOAP 版本 1.2 编码。

备注

XmlSerializer无法序列化以下内容: 的ArrayList数组和 的List<T>数组。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Serialize(XmlWriter, Object, XmlSerializerNamespaces)

Source:
XmlSerializer.cs
Source:
XmlSerializer.cs
Source:
XmlSerializer.cs

使用指定的 Object 和指定命名空间序列化指定的 XmlWriter 并将 XML 文档写入文件。

C#
public void Serialize (System.Xml.XmlWriter xmlWriter, object o, System.Xml.Serialization.XmlSerializerNamespaces namespaces);
C#
public void Serialize (System.Xml.XmlWriter xmlWriter, object? o, System.Xml.Serialization.XmlSerializerNamespaces? namespaces);

参数

xmlWriter
XmlWriter

用于编写 XML 文档的 XmlWriter

o
Object

要序列化的 Object

namespaces
XmlSerializerNamespaces

该对象所引用的 XmlSerializerNamespaces

例外

序列化期间发生错误。 使用 InnerException 属性时可使用原始异常。

示例

以下示例使用 XmlWriter序列化对象。 该示例还创建 , XmlSerializerNamespaces 并向 对象添加两个命名空间。 类的 XmlElementAttribute 多个实例应用于类成员,以指定每个元素的命名空间。

C#
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

// This is the class that will be serialized.
public class OrderedItem5
{
    [XmlElement(Namespace = "http://www.cpandl.com")]
    public string ItemName;
    [XmlElement(Namespace = "http://www.cpandl.com")]
    public string Description;
    [XmlElement(Namespace = "http://www.cohowinery.com")]
    public decimal UnitPrice;
    [XmlElement(Namespace = "http://www.cpandl.com")]
    public int Quantity;
    [XmlElement(Namespace = "http://www.cohowinery.com")]
    public decimal LineTotal;
    // A custom method used to calculate price per item.
    public void Calculate()
    {
        LineTotal = UnitPrice * Quantity;
    }
}

public class Test5
{
    public static void Main()
    {
        Test5 t = new();
        // Write a purchase order.
        t.SerializeObject("simple.xml");
    }

    private void SerializeObject(string filename)
    {
        Console.WriteLine("Writing With XmlTextWriter");

        XmlSerializer serializer = new(typeof(OrderedItem5));
        OrderedItem5 i = new()
        {
            ItemName = "Widget",
            Description = "Regular Widget",
            Quantity = 10,
            UnitPrice = (decimal)2.30
        };
        i.Calculate();

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

        // 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);
        XmlTextWriter writer = new(fs, new UTF8Encoding());

        // Serialize using the XmlTextWriter.
        serializer.Serialize(writer, i, ns);
        writer.Close();
    }
}
XML
<?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>

注解

Serialize调用 方法时,对象的公共字段和读/写属性将转换为 XML。 不序列化方法、索引器、私有字段和只读属性。

xmlWriter使用 参数指定派生自抽象XmlWriter类的对象,该类旨在编写 XML 文档。 XmlTextWriter派生自 XmlWriter

备注

XmlSerializer无法序列化以下内容: 的ArrayList数组和 的List<T>数组。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

Serialize(TextWriter, Object, XmlSerializerNamespaces)

Source:
XmlSerializer.cs
Source:
XmlSerializer.cs
Source:
XmlSerializer.cs

使用指定的 Object 和指定命名空间序列化指定的 TextWriter 并将 XML 文档写入文件。

C#
public void Serialize (System.IO.TextWriter textWriter, object o, System.Xml.Serialization.XmlSerializerNamespaces namespaces);
C#
public void Serialize (System.IO.TextWriter textWriter, object? o, System.Xml.Serialization.XmlSerializerNamespaces? namespaces);

参数

textWriter
TextWriter

用于编写 XML 文档的 TextWriter

o
Object

要序列化的 Object

namespaces
XmlSerializerNamespaces

包含生成的 XML 文档的命名空间的 XmlSerializerNamespaces

例外

序列化期间发生错误。 使用 InnerException 属性时可使用原始异常。

示例

以下示例使用 TextWriter序列化对象。 该示例还创建一个 对象,并将两个 XmlSerializerNamespaces 命名空间添加到 该对象。 定义序列化对象的类也使用 XmlElementAttribute 属性进行特性化,以指定每个元素的命名空间。

C#
using System;
using System.IO;
using System.Xml.Serialization;

// This is the class that will be serialized.
public class OrderedItem1
{
    [XmlElement(Namespace = "http://www.cpandl.com")]
    public string ItemName;
    [XmlElement(Namespace = "http://www.cpandl.com")]
    public string Description;
    [XmlElement(Namespace = "http://www.cohowinery.com")]
    public decimal UnitPrice;
    [XmlElement(Namespace = "http://www.cpandl.com")]
    public int Quantity;
    [XmlElement(Namespace = "http://www.cohowinery.com")]
    public decimal LineTotal;
    // A custom method used to calculate price per item.
    public void Calculate()
    {
        LineTotal = UnitPrice * Quantity;
    }
}

public class Test1
{
    public static void Main(string[] args)
    {
        Test1 t = new();
        // Write a purchase order.
        t.SerializeObject("simple.xml");
    }

    private void SerializeObject(string filename)
    {
        Console.WriteLine("Writing With TextWriter");
        // Create an XmlSerializer instance using the type.
        XmlSerializer serializer = new(typeof(OrderedItem1));
        OrderedItem1 i = new()
        {
            ItemName = "Widget",
            Description = "Regular Widget",
            Quantity = 10,
            UnitPrice = (decimal)2.30
        };
        i.Calculate();

        // Create an XmlSerializerNamespaces object.
        XmlSerializerNamespaces ns = new();
        // Add two namespaces with prefixes.
        ns.Add("inventory", "http://www.cpandl.com");
        ns.Add("money", "http://www.cohowinery.com");
        // Create a StreamWriter to write with.
        StreamWriter writer = new(filename);
        /* Serialize using the object using the TextWriter
        and namespaces. */
        serializer.Serialize(writer, i, ns);
        writer.Close();
    }
}
XML
<?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>

注解

Serialize调用 方法时,对象的公共字段和读/写属性将转换为 XML。 不序列化方法、索引器、私有字段和只读属性。

textWriter使用 参数指定派生自抽象TextWriter类的对象。 派生自 TextWriter 类的类包括:

备注

XmlSerializer无法序列化以下内容: 的 ArrayList 数组和 的List<T>数组。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

Serialize(Object, XmlSerializationWriter)

Source:
XmlSerializer.cs
Source:
XmlSerializer.cs
Source:
XmlSerializer.cs

使用指定的 Object 序列化指定的 XmlSerializationWriter 并将 XML 文档写入文件。

C#
protected virtual void Serialize (object? o, System.Xml.Serialization.XmlSerializationWriter writer);
C#
protected virtual void Serialize (object o, System.Xml.Serialization.XmlSerializationWriter writer);

参数

o
Object

要序列化的 Object

writer
XmlSerializationWriter

用于编写 XML 文档的 XmlSerializationWriter

例外

当未在子类中重写该方法时,为访问该方法进行的任何尝试。

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Serialize(TextWriter, Object)

Source:
XmlSerializer.cs
Source:
XmlSerializer.cs
Source:
XmlSerializer.cs

使用指定的 Object 序列化指定的 TextWriter 并将 XML 文档写入文件。

C#
public void Serialize (System.IO.TextWriter textWriter, object o);
C#
public void Serialize (System.IO.TextWriter textWriter, object? o);

参数

textWriter
TextWriter

用于编写 XML 文档的 TextWriter

o
Object

要序列化的 Object

示例

以下示例使用 TextWriter序列化对象。

C#
using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;

// This is the class that will be serialized.
public class OrderedItem
{
    public string ItemName;
    public string Description;
    public decimal UnitPrice;
    public int Quantity;
    public decimal LineTotal;
    // A custom method used to calculate price per item.
    public void Calculate()
    {
        LineTotal = UnitPrice * Quantity;
    }
}

public class Test
{
    public static void Main(string[] args)
    {
        Test t = new();
        // Write a purchase order.
        t.SerializeObject("simple.xml");
    }

    private void SerializeObject(string filename)
    {
        Console.WriteLine("Writing With TextWriter");

        XmlSerializer serializer = new(typeof(OrderedItem));
        OrderedItem i = new()
        {
            ItemName = "Widget",
            Description = "Regular Widget",
            Quantity = 10,
            UnitPrice = (decimal)2.30
        };
        i.Calculate();

        /* Create a StreamWriter to write with. First create a FileStream
           object, and create the StreamWriter specifying an Encoding to use. */
        FileStream fs = new(filename, FileMode.Create);
        StreamWriter writer = new(fs, new UTF8Encoding());
        // Serialize using the XmlTextWriter.
        serializer.Serialize(writer, i);
        writer.Close();
    }
}
XML
<?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>

注解

方法 Serialize 将对象的公共字段和读/写属性转换为 XML。 它不转换方法、索引器、专用字段或只读属性。

在 参数中 textWriter ,指定派生自抽象 TextWriter 类的对象。 派生自 TextWriter 的类包括:

备注

XmlSerializer无法序列化以下内容: 的 ArrayList 数组和 的List<T>数组。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

Serialize(Stream, Object)

Source:
XmlSerializer.cs
Source:
XmlSerializer.cs
Source:
XmlSerializer.cs

使用指定的 Object 序列化指定的 Stream 并将 XML 文档写入文件。

C#
public void Serialize (System.IO.Stream stream, object o);
C#
public void Serialize (System.IO.Stream stream, object? o);

参数

stream
Stream

用于编写 XML 文档的 Stream

o
Object

要序列化的 Object

例外

序列化期间发生错误。 使用 InnerException 属性时可使用原始异常。

示例

以下示例使用 Stream 对象序列化对象。

C#
using System;
using System.IO;
using System.Xml.Serialization;

// This is the class that will be serialized.
public class OrderedItem2
{
    public string ItemName;
    public string Description;
    public decimal UnitPrice;
    public int Quantity;
    public decimal LineTotal;

    // A custom method used to calculate price per item.
    public void Calculate()
    {
        LineTotal = UnitPrice * Quantity;
    }
}

public class Test2
{
    public static void Main(string[] args)
    {
        Test2 t = new();
        // Write a purchase order.
        t.SerializeObject("simple.xml");
    }

    private void SerializeObject(string filename)
    {
        Console.WriteLine("Writing With Stream");

        XmlSerializer serializer = new(typeof(OrderedItem2));
        OrderedItem2 i = new()
        {
            ItemName = "Widget",
            Description = "Regular Widget",
            Quantity = 10,
            UnitPrice = (decimal)2.30
        };
        i.Calculate();

        // Create a FileStream to write with.
        FileStream writer = new(filename, FileMode.Create);
        // Serialize the object, and close the TextWriter
        serializer.Serialize(writer, i);
        writer.Close();
    }
}
XML
<?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>

注解

方法 Serialize 将对象的公共字段和读/写属性转换为 XML。 它不转换方法、索引器、专用字段或只读属性。

在 参数中 stream ,指定派生自抽象 Stream 类的对象。 派生自 Stream 的类包括:

备注

XmlSerializer无法序列化以下内容: 的 ArrayList 数组和 的List<T>数组。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

Serialize(Stream, Object, XmlSerializerNamespaces)

Source:
XmlSerializer.cs
Source:
XmlSerializer.cs
Source:
XmlSerializer.cs

使用引用指定命名空间的指定 Stream 序列化指定的 Object 并将 XML 文档写入文件。

C#
public void Serialize (System.IO.Stream stream, object o, System.Xml.Serialization.XmlSerializerNamespaces namespaces);
C#
public void Serialize (System.IO.Stream stream, object? o, System.Xml.Serialization.XmlSerializerNamespaces? namespaces);

参数

stream
Stream

用于编写 XML 文档的 Stream

o
Object

要序列化的 Object

namespaces
XmlSerializerNamespaces

该对象所引用的 XmlSerializerNamespaces

例外

序列化期间发生错误。 使用 InnerException 属性时可使用原始异常。

示例

以下示例使用 Stream 对象序列化对象。 该示例还创建 并将两个 XmlSerializerNamespaces 命名空间添加到 对象。 定义序列化对象的类也使用 XmlElementAttribute 特性进行特性化,以指定每个元素的命名空间。

C#
using System;
using System.IO;
using System.Xml.Serialization;

// This is the class that will be serialized.
public class OrderedItem3
{
    [XmlElement(Namespace = "http://www.cpandl.com")]
    public string ItemName;
    [XmlElement(Namespace = "http://www.cpandl.com")]
    public string Description;
    [XmlElement(Namespace = "http://www.cohowinery.com")]
    public decimal UnitPrice;
    [XmlElement(Namespace = "http://www.cpandl.com")]
    public int Quantity;
    [XmlElement(Namespace = "http://www.cohowinery.com")]
    public decimal LineTotal;

    // A custom method used to calculate price per item.
    public void Calculate()
    {
        LineTotal = UnitPrice * Quantity;
    }
}

public class Test3
{
    public static void Main()
    {
        Test3 t = new();
        // Write a purchase order.
        t.SerializeObject("simple.xml");
        t.DeserializeObject("simple.xml");
    }

    private void SerializeObject(string filename)
    {
        Console.WriteLine("Writing With Stream");

        XmlSerializer serializer =
            new(typeof(OrderedItem3));

        OrderedItem3 i = new()
        {
            ItemName = "Widget",
            Description = "Regular Widget",
            Quantity = 10,
            UnitPrice = (decimal)2.30
        };
        i.Calculate();

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

        // Add two prefix-namespace pairs.
        ns.Add("inventory", "http://www.cpandl.com");
        ns.Add("money", "http://www.cohowinery.com");

        // Create a FileStream to write with.
        FileStream writer = new(filename, FileMode.Create);

        // Serialize the object, and close the TextWriter
        serializer.Serialize(writer, i, ns);
        writer.Close();
    }

    private void DeserializeObject(string filename)
    {
        Console.WriteLine("Reading with Stream");
        // Create an instance of the XmlSerializer.
        XmlSerializer serializer = new(typeof(OrderedItem3));

        // Writing the file requires a Stream.
        Stream reader = new FileStream(filename, FileMode.Open);

        // Declare an object variable of the type to be deserialized.
        OrderedItem3 i;

        /* Use the Deserialize method to restore the object's state
           using data from the XML document. */
        i = (OrderedItem3)serializer.Deserialize(reader);

        // Write out the properties of the object.
        Console.Write(
            i.ItemName + "\t" +
            i.Description + "\t" +
            i.UnitPrice + "\t" +
            i.Quantity + "\t" +
            i.LineTotal);
    }
}

注解

Serialize调用 方法时,对象的公共字段和读/写属性将转换为 XML。 方法、索引器、专用字段和只读属性不会序列化。

stream使用 参数指定派生自抽象Stream类的对象,该类旨在写入流。 派生自 类的 Stream 类包括:

备注

XmlSerializer无法序列化以下内容: 的 ArrayList 数组和 的List<T>数组。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0