Export (0) Print
Expand All

XmlSerializer.CanDeserialize Method

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

Namespace: System.Xml.Serialization
Assembly: System.Xml (in system.xml.dll)

public virtual bool CanDeserialize (
	XmlReader xmlReader
)
public boolean CanDeserialize (
	XmlReader xmlReader
)
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 that the XmlReader points to; otherwise, false.

The following example calls the CanDeserialize method to check whether an XML document can be deserialized.

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();
}


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();
} //TestDocument

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft