Export (0) Print
Expand All

XmlSerializer.CanDeserialize Method

Gets a value indicating whether this XmlSerializer can deserialize a specified XML document.

[Visual Basic]
Public Overridable Function CanDeserialize( _
   ByVal xmlReader As XmlReader _
) As Boolean
[C#]
public virtual bool CanDeserialize(
 XmlReader xmlReader
);
[C++]
public: virtual bool CanDeserialize(
 XmlReader* xmlReader
);
[JScript]
public function CanDeserialize(
   xmlReader : XmlReader
) : Boolean;

Parameters

xmlReader
An XmlReader that points to the document to deserialize.

Return Value

true if this XmlSerializer can deserialize the object which the XmlReader points to; otherwise, false.

Example

[Visual Basic, C#, C++] The following example calls the CanDeserialize method to check whether an XML document can be deserialized.

[Visual Basic] 
Private Sub TestDocument _
    (ByVal filename As String, _
     ByVal objType As Type)
        ' Using a FileStream, create an XmlTextReader.
        Dim fs As New FileStream(filename, FileMode.Open)
        Dim reader As New XmlTextReader(fs)
        Dim serializer As New XmlSerializer(objType)
        If serializer.CanDeserialize(reader) Then
            Dim o As Object = serializer.Deserialize(reader)
        End If
        fs.Close()
    End Sub

[C#] 
private void TestDocument(string filename, Type objType)
{
   // Using a FileStream, create an XmlTextReader.
   Stream fs = new FileStream(filename, FileMode.Open);
   XmlReader reader = new XmlTextReader(fs);
   XmlSerializer serializer = new XmlSerializer(objType);
   if(serializer.CanDeserialize(reader))
      {
         Object o = serializer.Deserialize(reader);
      }
   fs.Close();
}


[C++] 
private:
void TestDocument(String* filename, Type* objType)
{
   // Using a FileStream, create an XmlTextReader.
   Stream* fs = new FileStream(filename, FileMode::Open);
   XmlReader* reader = new XmlTextReader(fs);
   XmlSerializer* serializer = new XmlSerializer(objType);
   if(serializer->CanDeserialize(reader))
      {
         Object* o = serializer->Deserialize(reader);
      }
   fs->Close();
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

XmlSerializer Class | XmlSerializer Members | System.Xml.Serialization Namespace | Deserialize

Show:
© 2015 Microsoft