XmlSerializer.CanDeserialize(XmlReader) Method

Definition

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

C#
public virtual bool CanDeserialize(System.Xml.XmlReader xmlReader);

Parameters

xmlReader
XmlReader

An XmlReader that points to the document to deserialize.

Returns

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

Examples

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

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

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

See also