XmlSerializer.Serialize 方法

定義

將物件序列化成為 XML 文件。

多載

Serialize(XmlWriter, Object)

序列化指定的 Object,並使用指定的 XmlWriter 將 XML 文件寫入檔案。

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

序列化指定的 Object,並使用指定的 XmlWriter、XML 命名空間和編碼方式,將 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)

序列化指定的 Object,並使用指定的 Stream 將 XML 文件寫入檔案,以參考指定的命名空間。

Serialize(XmlWriter, Object)

來源:
XmlSerializer.cs
來源:
XmlSerializer.cs
來源:
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)

來源:
XmlSerializer.cs
來源:
XmlSerializer.cs
來源:
XmlSerializer.cs

序列化指定的 Object,並使用指定的 XmlWriter、XML 命名空間和編碼方式,將 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 識別碼的基底字串。 根據預設,這些是 「id1」、「id2」 等等。 但是,如果參數設定為 「myBase」,則產生的值為 「myBaseid1」、「myBaseid2」 等等。 這項功能可用來在整個 SOAP 訊息中建立唯一識別碼值。

適用於

.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)

來源:
XmlSerializer.cs
來源:
XmlSerializer.cs
來源:
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)

來源:
XmlSerializer.cs
來源:
XmlSerializer.cs
來源:
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)

來源:
XmlSerializer.cs
來源:
XmlSerializer.cs
來源:
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

XmlSerializerNamespaces,包含產生之 XML 文件的命名空間。

例外狀況

在序列化期間發生錯誤。 可以使用 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)

來源:
XmlSerializer.cs
來源:
XmlSerializer.cs
來源:
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)

來源:
XmlSerializer.cs
來源:
XmlSerializer.cs
來源:
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)

來源:
XmlSerializer.cs
來源:
XmlSerializer.cs
來源:
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)

來源:
XmlSerializer.cs
來源:
XmlSerializer.cs
來源:
XmlSerializer.cs

序列化指定的 Object,並使用指定的 Stream 將 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