DataContractSerializer.IsStartObject Método

Definición

Determina si el lector está colocado en un objeto que se puede deserializar.

Sobrecargas

Nombre Description
IsStartObject(XmlReader)

Determina si XmlReader está situado en un objeto que se puede deserializar.

IsStartObject(XmlDictionaryReader)

Determina si XmlDictionaryReader está situado en un objeto que se puede deserializar.

IsStartObject(XmlReader)

Determina si XmlReader está situado en un objeto que se puede deserializar.

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

Parámetros

reader
XmlReader

que XmlReader se usa para leer la secuencia XML.

Devoluciones

true si el lector está en el elemento inicial de la secuencia que se va a leer; de lo contrario, false.

Se aplica a

IsStartObject(XmlDictionaryReader)

Determina si XmlDictionaryReader está situado en un objeto que se puede deserializar.

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

Parámetros

reader
XmlDictionaryReader

que XmlDictionaryReader se usa para leer la secuencia XML.

Devoluciones

true si el lector está en el elemento inicial de la secuencia que se va a leer; de lo contrario, false.

Ejemplos

En el ejemplo siguiente se usa la IsStartObject propiedad para determinar si se ha encontrado el inicio de los datos.

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

Comentarios

IsStartObject determina si puede leer un objeto comprobando que está colocado en un elemento XML. También examina el nombre y el espacio de nombres del elemento XML en el que se coloca el lector y compara los valores con el nombre y el espacio de nombres esperados. El nombre y el espacio de nombres esperados se pueden establecer con lo siguiente: el nombre del contrato de datos y el espacio de nombres del tipo pasados al constructor, o los rootName valores y rootNamespace pasados al constructor (si están presentes).

Se aplica a