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
Not applicable.

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 Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2015 Microsoft