DataContractSerializer.IsStartObject 方法

定義

判斷讀取器是否置於可還原序列化的物件上。

多載

IsStartObject(XmlReader)

判斷 XmlReader 是否置於可還原序列化的物件上。

IsStartObject(XmlDictionaryReader)

判斷 XmlDictionaryReader 是否置於可還原序列化的物件上。

IsStartObject(XmlReader)

來源:
DataContractSerializer.cs
來源:
DataContractSerializer.cs
來源:
DataContractSerializer.cs

判斷 XmlReader 是否置於可還原序列化的物件上。

public:
 override bool IsStartObject(System::Xml::XmlReader ^ reader);
public override bool IsStartObject (System.Xml.XmlReader reader);
override this.IsStartObject : System.Xml.XmlReader -> bool
Public Overrides Function IsStartObject (reader As XmlReader) As Boolean

參數

reader
XmlReader

用來讀取 XML 資料流的 XmlReader

傳回

如果讀取器位於要讀取之資料流的開始項目,則為 true,否則為 false

適用於

IsStartObject(XmlDictionaryReader)

來源:
DataContractSerializer.cs
來源:
DataContractSerializer.cs
來源:
DataContractSerializer.cs

判斷 XmlDictionaryReader 是否置於可還原序列化的物件上。

public:
 override bool IsStartObject(System::Xml::XmlDictionaryReader ^ reader);
public override bool IsStartObject (System.Xml.XmlDictionaryReader reader);
override this.IsStartObject : System.Xml.XmlDictionaryReader -> bool
Public Overrides Function IsStartObject (reader As XmlDictionaryReader) As Boolean

參數

reader
XmlDictionaryReader

用於讀取 XML 資料流的 XmlDictionaryReader

傳回

如果讀取器位於要讀取之資料流的開始項目,則為 true,否則為 false

範例

下列範例會使用 IsStartObject 屬性來判斷是否已找到資料的開始項目。

public static void ReadObjectData(string path)
{
    // Create the reader.
    FileStream fs = new FileStream(path, FileMode.Open);
    XmlDictionaryReader reader =
        XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas());

    // Create the DataContractSerializer specifying the type,
    // root and namespace to use. The root value corresponds
    // to the DataContract.Name value, and the namespace value
    // corresponds to the DataContract.Namespace value.
    DataContractSerializer ser =
        new DataContractSerializer(typeof(Person),
        "Customer", @"http://www.contoso.com");

    // Test if the serializer is on the start of the
    // object data. If so, read the data and write it
    // to the console.
    while (reader.Read())
    {
        if (ser.IsStartObject(reader))
        {
            Console.WriteLine("Found the element");
            Person p = (Person)ser.ReadObject(reader);
            Console.WriteLine("{0} {1}    id:{2}",
                p.FirstName, p.LastName, p.ID);
        }

        Console.WriteLine(reader.Name);
        break;
    }
    fs.Flush();
    fs.Close();
}
Public Shared Sub ReadObjectData(ByVal path As String) 
    ' Create the reader.
    Dim fs As New FileStream(path, FileMode.Open)
    Dim reader As XmlDictionaryReader = _
        XmlDictionaryReader.CreateTextReader(fs, New XmlDictionaryReaderQuotas())
    
    ' Create the DataContractSerializer specifying the type, 
    ' root and namespace to use. The root value corresponds
    ' to the DataContract.Name value, and the namespace value
    ' corresponds to the DataContract.Namespace value.
    Dim ser As New DataContractSerializer(GetType(Person), _
        "Customer", "http://www.contoso.com")
    
    ' Test if the serializer is on the start of the 
    ' object data. If so, read the data and write it 
    ' to the console.
    While reader.Read()
        If ser.IsStartObject(reader) Then
            Console.WriteLine("Found the element")
            Dim p As Person = CType(ser.ReadObject(reader), Person)
            Console.WriteLine("{0} {1}    id:{2}", p.FirstName, p.LastName, p.ID)
        End If
                
        Console.WriteLine(reader.Name)
    End While
    
    fs.Flush()
    fs.Close()

End Sub

備註

IsStartObject 會判斷它是否可以在檢查物件是否置於 XML 項目時讀取該物件。 它也會檢查讀取器所在之 XML 項目的名稱和命名空間,並比較值與預期的名稱和命名空間。 預期的名稱和命名空間可以使用下列項目進行設定:傳入建構函式之型別的資料合約名稱和命名空間,或是傳入建構函式的 rootNamerootNamespace 值 (如果有的話)。

適用於