XmlSerializer.Deserialize 方法

定義

反序列化 XML 文件。

多載

名稱 Description
Deserialize(Stream)

將指定的 StreamXML 文件反序列化。

Deserialize(TextReader)

將指定的 TextReaderXML 文件反序列化。

Deserialize(XmlSerializationReader)

將指定的 XmlSerializationReaderXML 文件反序列化。

Deserialize(XmlReader)

將指定的 XmlReaderXML 文件反序列化。

Deserialize(XmlReader, String)

反序列化由指定 XmlReader 與編碼風格所包含的 XML 文件。

Deserialize(XmlReader, XmlDeserializationEvents)

反序列化由指定 XmlReader 文件所包含的 XML 文件,並允許覆蓋反序列化過程中發生的事件。

Deserialize(XmlReader, String, XmlDeserializationEvents)

利用指定 XmlReader資料將物件反序列化。

Deserialize(Stream)

將指定的 StreamXML 文件反序列化。

public:
 System::Object ^ Deserialize(System::IO::Stream ^ stream);
public object Deserialize(System.IO.Stream stream);
member this.Deserialize : System.IO.Stream -> obj
Public Function Deserialize (stream As Stream) As Object

參數

stream
Stream

那個 Stream 包含要反序列化的 XML 文件。

傳回

這個 Object 存在被解開了序列。

範例

以下範例是利用物件 Stream 反序列化物件。

using System;
using System.IO;
using System.Xml.Serialization;

// This is the class that will be deserialized.
public class OrderedItem
{
    [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 Test
{
    public static void Main()
    {
        Test t = new Test();
        // Read a purchase order.
        t.DeserializeObject("simple.xml");
    }

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

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

        using (Stream reader = new FileStream(filename, FileMode.Open))
        {
            // Call the Deserialize method to restore the object's state.
            i = (OrderedItem)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);
    }
}
Imports System.IO
Imports System.Xml.Serialization

' This is the class that will be deserialized.
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()
        ' Read a purchase order.
        t.DeserializeObject("simple.xml")
    End Sub
        
    Private Sub DeserializeObject(ByVal filename As String)
        Console.WriteLine("Reading with Stream")
        ' Create an instance of the XmlSerializer.
        Dim serializer As New XmlSerializer(GetType(OrderedItem))       
        
        ' Declare an object variable of the type to be deserialized.
        Dim i As OrderedItem

        Using reader As New Filestream(filename, FileMode.Open)

            ' Call the Deserialize method to restore the object's state.
            i = CType(serializer.Deserialize(reader), OrderedItem)
        End Using

        ' Write out the properties of the object.
        Console.Write(i.ItemName & ControlChars.Tab & _
                      i.Description & ControlChars.Tab & _
                      i.UnitPrice & ControlChars.Tab & _
                      i.Quantity & ControlChars.Tab & _
                      i.LineTotal)
    End Sub
End Class
<?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>

備註

反序列化是指讀取 XML 文件並建構一個物件,該物件的型別與該文件的 XML 架構(XSD)緊密相符。

在反序列化之前,必須先用被反序列化物件的型別來建構 a XmlSerializer

使用參數 stream 指定一個源自類別 Stream 的物件,該類別設計用來寫入串流。 衍生自該 Stream 類別的類別包括:

Note

無法 XmlSerializer 反序列化以下的:的 ArrayList 陣列與的 List<T>陣列。

另請參閱

適用於

Deserialize(TextReader)

將指定的 TextReaderXML 文件反序列化。

public:
 System::Object ^ Deserialize(System::IO::TextReader ^ textReader);
public object Deserialize(System.IO.TextReader textReader);
member this.Deserialize : System.IO.TextReader -> obj
Public Function Deserialize (textReader As TextReader) As Object

參數

textReader
TextReader

那個 TextReader 包含要反序列化的 XML 文件。

傳回

這個 Object 存在被解開了序列。

例外狀況

在解序過程中發生錯誤。 原始例外可用該 InnerException 屬性。

範例

以下範例是利用物件 TextReader 反序列化物件。

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

// This is the class that will be deserialized.
public class OrderedItem
{
    [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 Test
{
   public static void Main()
    {
        Test t = new Test();
        // Read a purchase order.
        t.DeserializeObject("simple.xml");
    }

    private void DeserializeObject(string filename)
    {
        Console.WriteLine("Reading with TextReader");

        // Create an instance of the XmlSerializer specifying type.
        XmlSerializer serializer =
        new XmlSerializer(typeof(OrderedItem));

        // Create a TextReader to read the file.
        FileStream fs = new FileStream(filename, FileMode.OpenOrCreate);
        TextReader reader = new StreamReader(fs);

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

        // Use the Deserialize method to restore the object's state.
        i = (OrderedItem) 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);
    }
}
Imports System.IO
Imports System.Text
Imports System.Xml.Serialization

' This is the class that will be deserialized.
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()
        ' Read a purchase order.
        t.DeserializeObject("simple.xml")
    End Sub
    
    Private Sub DeserializeObject(filename As String)
        Console.WriteLine("Reading with TextReader")
        
        ' Create an instance of the XmlSerializer specifying type.
        Dim serializer As New XmlSerializer(GetType(OrderedItem))
        
        ' Create a TextReader to read the file. 
        Dim fs as New FileStream(filename, FileMode.OpenOrCreate)
        Dim reader As New StreamReader(fs)
        
        ' Declare an object variable of the type to be deserialized.
        Dim i As OrderedItem
        
        ' Use the Deserialize method to restore the object's state.
        i = CType(serializer.Deserialize(reader), OrderedItem)
        
        ' Write out the properties of the object.
        Console.Write(i.ItemName & ControlChars.Tab & _
                      i.Description & ControlChars.Tab & _
                      i.UnitPrice & ControlChars.Tab & _
                      i.Quantity & ControlChars.Tab & _
                      i.LineTotal)
    End Sub
End Class

備註

反序列化是讀取 XML 文件實例並建構一個物件,該物件強型別對應該文件的 XML Schema(XSD)。

在反序列化之前,必須先用被反序列化物件的型別來建構 a XmlSerializer

繼承自 TextReader 包含 StringReaderStreamReader的類別。 如果你使用 a StreamReader 來反序列化物件,必須用適當的 Encoding來構造 。StreamReader XML 文件指定的編碼被忽略。

Note

若要使用 XML 文件指定的編碼,請使用 Deserialize 取 的 XmlReader 過載。 它 XmlReader 會自動偵測並使用 XML 文件中指定的編碼方式。

Note

無法 XmlSerializer 反序列化以下的:的 ArrayList 陣列與的 List<T>陣列。

另請參閱

適用於

Deserialize(XmlSerializationReader)

將指定的 XmlSerializationReaderXML 文件反序列化。

protected:
 virtual System::Object ^ Deserialize(System::Xml::Serialization::XmlSerializationReader ^ reader);
protected virtual object Deserialize(System.Xml.Serialization.XmlSerializationReader reader);
abstract member Deserialize : System.Xml.Serialization.XmlSerializationReader -> obj
override this.Deserialize : System.Xml.Serialization.XmlSerializationReader -> obj
Protected Overridable Function Deserialize (reader As XmlSerializationReader) As Object

參數

reader
XmlSerializationReader

那個 XmlSerializationReader 包含要反序列化的 XML 文件。

傳回

去序列化的物件。

例外狀況

當方法未在後代類別中被覆寫時,任何嘗試存取該方法的行為都會被進行。

適用於

Deserialize(XmlReader)

將指定的 XmlReaderXML 文件反序列化。

public:
 System::Object ^ Deserialize(System::Xml::XmlReader ^ xmlReader);
public object Deserialize(System.Xml.XmlReader xmlReader);
member this.Deserialize : System.Xml.XmlReader -> obj
Public Function Deserialize (xmlReader As XmlReader) As Object

參數

xmlReader
XmlReader

那個 XmlReader 包含要反序列化的 XML 文件。

傳回

這個 Object 存在被解開了序列。

例外狀況

在解序過程中發生錯誤。 原始例外可用該 InnerException 屬性。

範例

以下範例使用 XmlReader

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

// This is the class that will be deserialized.
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 Test();
        // Read a purchase order.
        t.DeserializeObject("simple.xml");
    }

    private void DeserializeObject(string filename)
    {
        Console.WriteLine("Reading with XmlReader");

        // Create an instance of the XmlSerializer specifying type and namespace.
        XmlSerializer serializer = new
        XmlSerializer(typeof(OrderedItem));

        // A FileStream is needed to read the XML document.
        FileStream fs = new FileStream(filename, FileMode.Open);
        XmlReader reader = XmlReader.Create(fs);

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

        // Use the Deserialize method to restore the object's state.
        i = (OrderedItem)serializer.Deserialize(reader);
        fs.Close();

        // Write out the properties of the object.
        Console.Write(
        i.ItemName + "\t" +
        i.Description + "\t" +
        i.UnitPrice + "\t" +
        i.Quantity + "\t" +
        i.LineTotal);
    }
}
Imports System.IO
Imports System.Text
Imports System.Xml
Imports System.Xml.Serialization

' This is the class that will be deserialized.
Public Class OrderedItem
    Public ItemName As String
    Public Description As String
    Public UnitPrice As Decimal
    Public Quantity As Integer
    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()
        ' Read a purchase order.
        t.DeserializeObject("simple.xml")
    End Sub
      
    Private Sub DeserializeObject(ByVal filename As String)
        Console.WriteLine("Reading with XmlReader")
        
        ' Create an instance of the XmlSerializer specifying type and namespace.
        Dim serializer As New XmlSerializer(GetType(OrderedItem))
        
        ' A FileStream is needed to read the XML document.
        Dim fs As New FileStream(filename, FileMode.Open)
        Dim reader As XmlReader = XmlReader.Create(fs)
        
        ' Declare an object variable of the type to be deserialized.
        Dim i As OrderedItem
        
        ' Use the Deserialize method to restore the object's state.
        i = CType(serializer.Deserialize(reader), OrderedItem)
        fs.Close()

        ' Write out the properties of the object.
        Console.Write(i.ItemName & ControlChars.Tab & _
                      i.Description & ControlChars.Tab & _
                      i.UnitPrice & ControlChars.Tab & _
                      i.Quantity & ControlChars.Tab & _
                      i.LineTotal)
    End Sub
End Class
<?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>

備註

反序列化是讀取 XML 文件實例並建構一個物件,該物件強型別對應該文件的 XML Schema(XSD)。

在反序列化之前,必須先用被反序列化物件的型別來建構 a XmlSerializer

XmlReader 會自動偵測並使用 XML 文件中指定的編碼方式。

Note

無法 XmlSerializer 反序列化以下的:的 ArrayList 陣列與的 List<T>陣列。

另請參閱

適用於

Deserialize(XmlReader, String)

反序列化由指定 XmlReader 與編碼風格所包含的 XML 文件。

public:
 System::Object ^ Deserialize(System::Xml::XmlReader ^ xmlReader, System::String ^ encodingStyle);
public object Deserialize(System.Xml.XmlReader xmlReader, string encodingStyle);
member this.Deserialize : System.Xml.XmlReader * string -> obj
Public Function Deserialize (xmlReader As XmlReader, encodingStyle As String) As Object

參數

xmlReader
XmlReader

那個 XmlReader 包含要反序列化的 XML 文件。

encodingStyle
String

序列化 XML 的編碼風格。

傳回

去序列化的物件。

例外狀況

在解序過程中發生錯誤。 原始例外可用該 InnerException 屬性。

備註

反序列化是讀取 XML 文件實例並建構一個物件,該物件強型別對應該文件的 XML Schema(XSD)。

在反序列化之前,必須先用被反序列化物件的型別來建構 a XmlSerializer

將參數設 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>陣列。

另請參閱

適用於

Deserialize(XmlReader, XmlDeserializationEvents)

反序列化由指定 XmlReader 文件所包含的 XML 文件,並允許覆蓋反序列化過程中發生的事件。

public:
 System::Object ^ Deserialize(System::Xml::XmlReader ^ xmlReader, System::Xml::Serialization::XmlDeserializationEvents events);
public object Deserialize(System.Xml.XmlReader xmlReader, System.Xml.Serialization.XmlDeserializationEvents events);
member this.Deserialize : System.Xml.XmlReader * System.Xml.Serialization.XmlDeserializationEvents -> obj
Public Function Deserialize (xmlReader As XmlReader, events As XmlDeserializationEvents) As Object

參數

xmlReader
XmlReader

那個 XmlReader 包含要去序列化的文件。

events
XmlDeserializationEvents

XmlDeserializationEvents 類別的執行個體。

傳回

這個 Object 存在被解開了序列。

備註

物件正在解序化。

適用於

Deserialize(XmlReader, String, XmlDeserializationEvents)

利用指定 XmlReader資料將物件反序列化。

public:
 System::Object ^ Deserialize(System::Xml::XmlReader ^ xmlReader, System::String ^ encodingStyle, System::Xml::Serialization::XmlDeserializationEvents events);
public object Deserialize(System.Xml.XmlReader xmlReader, string encodingStyle, System.Xml.Serialization.XmlDeserializationEvents events);
member this.Deserialize : System.Xml.XmlReader * string * System.Xml.Serialization.XmlDeserializationEvents -> obj
Public Function Deserialize (xmlReader As XmlReader, encodingStyle As String, events As XmlDeserializationEvents) As Object

參數

xmlReader
XmlReader

用來讀取文件的類別實例 XmlReader

encodingStyle
String

所使用的編碼。

events
XmlDeserializationEvents

XmlDeserializationEvents 類別的執行個體。

傳回

物件正在解序化。

屬性

備註

此方法僅用於網路服務場景中未知標頭的反序列化。 此方法可避免 Web Service 方法中的事件同步。

適用於