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