IXmlSerializable.ReadXml(XmlReader) IXmlSerializable.ReadXml(XmlReader) IXmlSerializable.ReadXml(XmlReader) IXmlSerializable.ReadXml(XmlReader) Method

定義

從物件的 XML 表示產生該物件。Generates an object from its XML representation.

public:
 void ReadXml(System::Xml::XmlReader ^ reader);
public void ReadXml (System.Xml.XmlReader reader);
abstract member ReadXml : System.Xml.XmlReader -> unit
Public Sub ReadXml (reader As XmlReader)

參數

reader
XmlReader XmlReader XmlReader XmlReader

還原序列化物件的 XmlReader 資料流。The XmlReader stream from which the object is deserialized.

範例

下列範例說明ReadXml方法的執行。The following example illustrates an implementation of the ReadXml method.

virtual void ReadXml( XmlReader^ reader )
{
   personName = reader->ReadString();
}
public void ReadXml (XmlReader reader)
{
    personName = reader.ReadString();
}

下列範例說明如何使用XmlSerializer類別來還原序列化這個物件。The following example illustrates the use of the XmlSerializer class to deserialize this object.

#using <System.Xml.dll>
#using <System.dll>
#using <Person.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml::Serialization;

int main()
{
   XmlSerializer^ serializer = gcnew XmlSerializer( Person::typeid );
   FileStream^ file = gcnew FileStream( "test.xml",FileMode::Open );
   Person^ aPerson = dynamic_cast<Person^>(serializer->Deserialize( file ));
   Console::WriteLine( aPerson );
}
using System;
using System.IO;
using System.Xml.Serialization;

public class Reader {

  public static void Main() {
    XmlSerializer serializer = new XmlSerializer(typeof(Person));
    FileStream file = new FileStream("test.xml", FileMode.Open);
    Person aPerson = (Person) serializer.Deserialize(file);
    Console.WriteLine(aPerson);
  }

}

備註

方法必須使用WriteXml方法所撰寫的資訊來重建物件。 ReadXmlThe ReadXml method must reconstitute your object using the information that was written by the WriteXml method.

呼叫這個方法時, 讀取器會位於包裝類型資訊的開始標記上。When this method is called, the reader is positioned on the start tag that wraps the information for your type. 也就是, 直接在表示序列化物件開頭的開始標記上。That is, directly on the start tag that indicates the beginning of a serialized object. 當這個方法傳回時, 它必須從開頭到結尾讀取整個元素, 包括其所有內容。When this method returns, it must have read the entire element from beginning to end, including all of its contents. 不同于WriteXml方法, 架構不會自動處理包裝函式元素。Unlike the WriteXml method, the framework does not handle the wrapper element automatically. 您的執行必須這麼做。Your implementation must do so. 若無法觀察這些定位規則, 可能會導致程式碼產生非預期的執行時間例外狀況或損毀的資料。Failing to observe these positioning rules may cause code to generate unexpected runtime exceptions or corrupt data.

在執行此方法時, 您應該考慮惡意使用者可能會提供格式正確但不正確 XML 標記法, 以停用或改變應用程式的行為。When implementing this method, you should consider the possibility that a malicious user might provide a well-formed but invalid XML representation in order to disable or otherwise alter the behavior of your application.

適用於