共用方式為


IXmlSerializable.WriteXml(XmlWriter) 方法

定義

將物件轉換為其 XML 表示。

public:
 void WriteXml(System::Xml::XmlWriter ^ writer);
public void WriteXml (System.Xml.XmlWriter writer);
abstract member WriteXml : System.Xml.XmlWriter -> unit
Public Sub WriteXml (writer As XmlWriter)

參數

writer
XmlWriter

XmlWriter 資料流,物件會序列化至其中。

範例

下列範例說明 方法的實作 WriteXml

virtual void WriteXml( XmlWriter^ writer )
{
   writer->WriteString( personName );
}
public void WriteXml (XmlWriter writer)
{
    writer.WriteString(personName);
}

下列範例說明 XmlSerializer 如何使用 類別來還原序列化這個物件。

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

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

int main()
{
   // Create a person object.
   Person ^ fred = gcnew Person( "Fred Flintstone" );

   // Serialize the object to a file.
   XmlTextWriter^ writer = gcnew XmlTextWriter( "test.xml", nullptr );
   XmlSerializer^ serializer = gcnew XmlSerializer( Person::typeid );
   serializer->Serialize( writer, fred );
}
using System;
using System.Xml;
using System.Xml.Serialization;

public class Writer {

  public static void Main() {

    // Create a person object.
    Person fred = new Person("Fred Flintstone");

    // Serialize the object to a file.
    XmlTextWriter writer = new XmlTextWriter("test.xml", null);
    XmlSerializer serializer = new XmlSerializer(typeof(Person));
    serializer.Serialize(writer, fred);
  }
}

備註

您提供的實 WriteXml 作應該寫出 物件的 XML 標記法。 架構會寫入包裝函式專案,並將 XML 寫入器置於其開始之後。 您的實作可能會寫入其內容,包括子項目。 架構接著會關閉包裝函式專案。

將足夠的資訊寫入資料流程, XmlWriter 以允許 ReadXml 方法重新建構物件。

例如,如果您的物件狀態包含陣列變數,請務必寫入陣列的長度,或使用父元素來包含描述陣列值的元素,如此一來,您就知道在重新建構物件時要讀取的值數目。

適用於