This documentation is archived and is not being maintained.

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
public virtual bool CanDeserialize(
 XmlReader xmlReader
public: virtual bool CanDeserialize(
 XmlReader* xmlReader
public function CanDeserialize(
   xmlReader : XmlReader
) : Boolean;


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.


[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
    End Sub

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);
         Object o = serializer.Deserialize(reader);

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);
         Object* o = serializer->Deserialize(reader);

[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.


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