XmlSerializer.CanDeserialize(XmlReader) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取一个值,该值指示此 XmlSerializer 是否可以反序列化指定的 XML 文档。
public:
virtual bool CanDeserialize(System::Xml::XmlReader ^ xmlReader);
public virtual bool CanDeserialize (System.Xml.XmlReader xmlReader);
abstract member CanDeserialize : System.Xml.XmlReader -> bool
override this.CanDeserialize : System.Xml.XmlReader -> bool
Public Overridable Function CanDeserialize (xmlReader As XmlReader) As Boolean
参数
返回
如果此 XmlSerializer 可以反序列化 XmlReader 指向的对象,则为 true
,否则为 false
。
示例
以下示例调用该方法 CanDeserialize 来检查是否可以反序列化 XML 文档。
private:
void TestDocument( String^ filename, Type^ objType )
{
// Using a FileStream, create an XmlTextReader.
Stream^ fs = gcnew FileStream( filename,FileMode::Open );
XmlReader^ reader = gcnew XmlTextReader( fs );
XmlSerializer^ serializer = gcnew 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();
}
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