DataContractSerializer.IsStartObject Metodo

Definizione

Determina se il lettore è posizionato su un oggetto che può essere deserializzato.

Overload

Nome Descrizione
IsStartObject(XmlReader)

Determina se l'oggetto XmlReader è posizionato su un oggetto che può essere deserializzato.

IsStartObject(XmlDictionaryReader)

Determina se l'oggetto XmlDictionaryReader è posizionato su un oggetto che può essere deserializzato.

IsStartObject(XmlReader)

Determina se l'oggetto XmlReader è posizionato su un oggetto che può essere deserializzato.

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

Parametri

reader
XmlReader

Oggetto XmlReader utilizzato per leggere il flusso XML.

Valori restituiti

true se il lettore si trova all'elemento iniziale del flusso da leggere; in caso contrario, false.

Si applica a

IsStartObject(XmlDictionaryReader)

Determina se l'oggetto XmlDictionaryReader è posizionato su un oggetto che può essere deserializzato.

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

Parametri

reader
XmlDictionaryReader

Oggetto XmlDictionaryReader utilizzato per leggere il flusso XML.

Valori restituiti

true se il lettore si trova all'elemento iniziale del flusso da leggere; in caso contrario, false.

Esempio

Nell'esempio seguente viene utilizzata la IsStartObject proprietà per determinare se l'inizio dei dati è stato trovato.

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

Commenti

Determina IsStartObject se può leggere un oggetto controllando che sia posizionato su un elemento XML. Esamina inoltre il nome e lo spazio dei nomi dell'elemento XML in corrispondenza del quale il lettore è posizionato e confronta i valori con il nome e lo spazio dei nomi previsti. Il nome e lo spazio dei nomi previsti possono essere impostati con il seguente: nome del contratto dati e spazio dei nomi del tipo passato nel costruttore oppure i rootName valori e rootNamespace passati al costruttore (se presente).

Si applica a