XmlSerializer.Serialize メソッド (XmlWriter, Object)
アセンブリ: System.Xml (system.xml.dll 内)

Dim instance As XmlSerializer Dim xmlWriter As XmlWriter Dim o As Object instance.Serialize(xmlWriter, o)


Serialize メソッドは、オブジェクトのパブリック フィールドおよびパブリックな読み書き可能プロパティを XML に変換します。メソッド、インデクサ、プライベート フィールド、または読み取り専用プロパティは変換されません。オブジェクトのパブリックとプライベート両方のフィールドとプロパティをすべてシリアル化するには、BinaryFormatter を使用します。
xmlWriter パラメータには、抽象 XmlWriter クラスから派生したオブジェクトを指定します。XmlTextWriter は XmlWriter から派生します。
メモ 次のような ArrayList の配列および List の配列については、XmlSerializer でシリアル化することはできません。

XmlWriter を使用して、オブジェクトをシリアル化する例を次に示します。
Imports System Imports System.IO Imports System.Text Imports System.Xml Imports System.Xml.Serialization ' This is the class that will be serialized. Public Class OrderedItem Public ItemName As String Public Description As String Public UnitPrice As Decimal Public Quantity As Integer Public LineTotal As Decimal ' A custom method used to calculate price per item. Public Sub Calculate() LineTotal = UnitPrice * Quantity End Sub End Class Public Class Test Public Shared Sub Main() Dim t As New Test() ' Write a purchase order. t.SerializeObject("simple.xml") End Sub Private Sub SerializeObject(ByVal filename As String) Console.WriteLine("Writing With XmlTextWriter") Dim serializer As New XmlSerializer(GetType(OrderedItem)) Dim i As New OrderedItem() With i .ItemName = "Widget" .Description = "Regular Widget" .Quantity = 10 .UnitPrice = CDec(2.3) .Calculate() End With ' Create an XmlTextWriter using a FileStream. Dim fs As New FileStream(filename, FileMode.Create) Dim writer As New XmlTextWriter(fs, Encoding.Unicode) ' Serialize using the XmlTextWriter. serializer.Serialize(writer, i) writer.Close() End Sub End Class
using System; using System.IO; using System.Text; using System.Xml; using System.Xml.Serialization; // This is the class that will be serialized. public class OrderedItem { public string ItemName; public string Description; public decimal UnitPrice; public int Quantity; public decimal LineTotal; // A custom method used to calculate price per item. public void Calculate() { LineTotal = UnitPrice * Quantity; } } public class Test{ public static void Main() { Test t = new Test(); // Write a purchase order. t.SerializeObject("simple.xml"); } private void SerializeObject(string filename) { Console.WriteLine("Writing With XmlTextWriter"); XmlSerializer serializer = new XmlSerializer(typeof(OrderedItem)); OrderedItem i = new OrderedItem(); i.ItemName = "Widget"; i.Description = "Regular Widget"; i.Quantity = 10; i.UnitPrice = (decimal) 2.30; i.Calculate(); // Create an XmlTextWriter using a FileStream. Stream fs = new FileStream(filename, FileMode.Create); XmlWriter writer = new XmlTextWriter(fs, Encoding.Unicode); // Serialize using the XmlTextWriter. serializer.Serialize(writer, i); writer.Close(); } }
#using <System.Xml.dll> #using <System.dll> using namespace System; using namespace System::IO; using namespace System::Text; using namespace System::Xml; using namespace System::Xml::Serialization; // This is the class that will be serialized. public ref class OrderedItem { public: String^ ItemName; String^ Description; Decimal UnitPrice; int Quantity; Decimal LineTotal; // A custom method used to calculate price per item. void Calculate() { LineTotal = UnitPrice * Quantity; } }; void SerializeObject( String^ filename ) { Console::WriteLine( "Writing With XmlTextWriter" ); XmlSerializer^ serializer = gcnew XmlSerializer( OrderedItem::typeid ); OrderedItem^ i = gcnew OrderedItem; i->ItemName = "Widget"; i->Description = "Regular Widget"; i->Quantity = 10; i->UnitPrice = (Decimal)2.30; i->Calculate(); // Create an XmlTextWriter using a FileStream. Stream^ fs = gcnew FileStream( filename,FileMode::Create ); XmlWriter^ writer = gcnew XmlTextWriter( fs,Encoding::Unicode ); // Serialize using the XmlTextWriter. serializer->Serialize( writer, i ); writer->Close(); } int main() { // Write a purchase order. SerializeObject( "simple.xml" ); }
import System.*; import System.IO.*; import System.Text.*; import System.Xml.*; import System.Xml.Serialization.*; // This is the class that will be serialized. public class OrderedItem { public String itemName; public String description; public System.Decimal unitPrice; public int quantity; public System.Decimal lineTotal; // A custom method used to calculate price per item. public void Calculate() { lineTotal = Decimal.Multiply(unitPrice, Convert.ToDecimal(quantity)); } //Calculate } //OrderedItem public class Test { public static void main(String[] args) { Test t = new Test(); // Write a purchase order. t.SerializeObject("simple.xml"); } //main private void SerializeObject(String fileName) { Console.WriteLine("Writing With XmlTextWriter"); XmlSerializer serializer = new XmlSerializer(OrderedItem.class.ToType()); OrderedItem i = new OrderedItem(); i.itemName = "Widget"; i.description = "Regular Widget"; i.quantity = 10; i.unitPrice = Convert.ToDecimal(2.3); i.Calculate(); // Create an XmlTextWriter using a FileStream. Stream fs = new FileStream(fileName, FileMode.Create); XmlWriter writer = new XmlTextWriter(fs, Encoding.get_Unicode()); // Serialize using the XmlTextWriter. serializer.Serialize(writer, i); writer.Close(); } //SerializeObject } //Test
<?xml version="1.0"?> <OrderedItem xmlns:inventory="http://www.cpandl.com" xmlns:money="http://www.cohowinery.com"> <inventory:ItemName>Widget</inventory:ItemName> <inventory:Description>Regular Widget</inventory:Description> <money:UnitPrice>2.3</money:UnitPrice> <inventory:Quantity>10</inventory:Quantity> <money:LineTotal>23</money:LineTotal> </OrderedItem>

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


XmlSerializer.Serialize メソッド (Object, XmlSerializationWriter)
アセンブリ: System.Xml (system.xml.dll 内)



Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


XmlSerializer.Serialize メソッド (XmlWriter, Object, XmlSerializerNamespaces)
アセンブリ: System.Xml (system.xml.dll 内)

Public Sub Serialize ( _ xmlWriter As XmlWriter, _ o As Object, _ namespaces As XmlSerializerNamespaces _ )
Dim instance As XmlSerializer Dim xmlWriter As XmlWriter Dim o As Object Dim namespaces As XmlSerializerNamespaces instance.Serialize(xmlWriter, o, namespaces)
public function Serialize ( xmlWriter : XmlWriter, o : Object, namespaces : XmlSerializerNamespaces )


Serialize メソッドを呼び出すと、オブジェクトのパブリック フィールドとパブリックな読み書き可能プロパティが XML に変換されます。メソッド、インデクサ、プライベート フィールド、および読み取り専用プロパティはシリアル化されません。パブリックとプライベート両方のフィールドとプロパティをすべてシリアル化するには、BinaryFormatter を使用します。
xmlWriter パラメータを使用して、XML ドキュメントに書き込むようにデザインされている、抽象 XmlWriter クラスから派生したオブジェクトを指定します。XmlTextWriter は XmlWriter から派生します。
メモ 次のような ArrayList の配列および List の配列については、XmlSerializer でシリアル化することはできません。

XmlWriter を使用して、オブジェクトをシリアル化する例を次に示します。この例では、XmlSerializerNamespaces も作成し、そのオブジェクトに 2 つの名前空間を追加します。XmlElementAttribute クラスのいくつかのインスタンスが、各要素の名前空間を指定するためにクラス メンバに適用されています。
Imports System Imports System.IO Imports System.Text Imports System.Xml Imports System.Xml.Serialization ' This is the class that will be serialized. Public Class OrderedItem <XmlElement(Namespace := "http://www.cpandl.com")> _ Public ItemName As String <XmlElement(Namespace := "http://www.cpandl.com")> _ Public Description As String <XmlElement(Namespace := "http://www.cohowinery.com")> _ Public UnitPrice As Decimal <XmlElement(Namespace := "http://www.cpandl.com")> _ Public Quantity As Integer <XmlElement(Namespace := "http://www.cohowinery.com")> _ Public LineTotal As Decimal 'A custom method used to calculate price per item. Public Sub Calculate() LineTotal = UnitPrice * Quantity End Sub End Class Public Class Test Public Shared Sub Main() Dim t As New Test() ' Write a purchase order. t.SerializeObject("simple.xml") End Sub Private Sub SerializeObject(ByVal filename As String) Console.WriteLine("Writing With XmlTextWriter") Dim serializer As New XmlSerializer(GetType(OrderedItem)) Dim i As New OrderedItem() With i .ItemName = "Widget" .Description = "Regular Widget" .Quantity = 10 .UnitPrice = CDec(2.3) .Calculate() End With ' Create an XmlSerializerNamespaces object. Dim ns As New XmlSerializerNamespaces() ' Add two namespaces with prefixes. ns.Add("inventory", "http://www.cpandl.com") ns.Add("money", "http://www.cohowinery.com") ' Create an XmlTextWriter using a FileStream. Dim fs As New FileStream(filename, FileMode.Create) Dim writer As New XmlTextWriter(fs, New UTF8Encoding()) ' Serialize using the XmlTextWriter. serializer.Serialize(writer, i, ns) writer.Close() End Sub End Class
using System; using System.IO; using System.Text; using System.Xml; using System.Xml.Serialization; // This is the class that will be serialized. public class OrderedItem { [XmlElement(Namespace = "http://www.cpandl.com")] public string ItemName; [XmlElement(Namespace = "http://www.cpandl.com")] public string Description; [XmlElement(Namespace="http://www.cohowinery.com")] public decimal UnitPrice; [XmlElement(Namespace = "http://www.cpandl.com")] public int Quantity; [XmlElement(Namespace="http://www.cohowinery.com")] public decimal LineTotal; // A custom method used to calculate price per item. public void Calculate() { LineTotal = UnitPrice * Quantity; } } public class Test{ public static void Main() { Test t = new Test(); // Write a purchase order. t.SerializeObject("simple.xml"); } private void SerializeObject(string filename) { Console.WriteLine("Writing With XmlTextWriter"); XmlSerializer serializer = new XmlSerializer(typeof(OrderedItem)); OrderedItem i = new OrderedItem(); i.ItemName = "Widget"; i.Description = "Regular Widget"; i.Quantity = 10; i.UnitPrice = (decimal) 2.30; i.Calculate(); // Create an XmlSerializerNamespaces object. XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); // Add two namespaces with prefixes. ns.Add("inventory", "http://www.cpandl.com"); ns.Add("money", "http://www.cohowinery.com"); // Create an XmlTextWriter using a FileStream. Stream fs = new FileStream(filename, FileMode.Create); XmlWriter writer = new XmlTextWriter(fs, new UTF8Encoding()); // Serialize using the XmlTextWriter. serializer.Serialize(writer, i, ns); writer.Close(); } }
#using <System.Xml.dll> #using <System.dll> using namespace System; using namespace System::IO; using namespace System::Text; using namespace System::Xml; using namespace System::Xml::Serialization; // This is the class that will be serialized. public ref class OrderedItem { public: [XmlElement(Namespace="http://www.cpandl.com")] String^ ItemName; [XmlElement(Namespace="http://www.cpandl.com")] String^ Description; [XmlElement(Namespace="http://www.cohowinery.com")] Decimal UnitPrice; [XmlElement(Namespace="http://www.cpandl.com")] int Quantity; [XmlElement(Namespace="http://www.cohowinery.com")] Decimal LineTotal; // A custom method used to calculate price per item. void Calculate() { LineTotal = UnitPrice * Quantity; } }; void SerializeObject( String^ filename ) { Console::WriteLine( "Writing With XmlTextWriter" ); XmlSerializer^ serializer = gcnew XmlSerializer( OrderedItem::typeid ); OrderedItem^ i = gcnew OrderedItem; i->ItemName = "Widget"; i->Description = "Regular Widget"; i->Quantity = 10; i->UnitPrice = (Decimal)2.30; i->Calculate(); // Create an XmlSerializerNamespaces object. XmlSerializerNamespaces^ ns = gcnew XmlSerializerNamespaces; // Add two namespaces with prefixes. ns->Add( "inventory", "http://www.cpandl.com" ); ns->Add( "money", "http://www.cohowinery.com" ); // Create an XmlTextWriter using a FileStream. Stream^ fs = gcnew FileStream( filename,FileMode::Create ); XmlWriter^ writer = gcnew XmlTextWriter( fs,gcnew UTF8Encoding ); // Serialize using the XmlTextWriter. serializer->Serialize( writer, i, ns ); writer->Close(); } int main() { // Write a purchase order. SerializeObject( "simple.xml" ); }
import System.*; import System.IO.*; import System.Text.*; import System.Xml.*; import System.Xml.Serialization.*; // This is the class that will be serialized. public class OrderedItem { /** @attribute XmlElement(Namespace = "http://www.cpandl.com") */ public String itemName; /** @attribute XmlElement(Namespace = "http://www.cpandl.com") */ public String description; /** @attribute XmlElement(Namespace = "http://www.cohowinery.com") */ public System.Decimal unitPrice; /** @attribute XmlElement(Namespace = "http://www.cpandl.com") */ public int quantity; /** @attribute XmlElement(Namespace = "http://www.cohowinery.com") */ public System.Decimal lineTotal; // A custom method used to calculate price per item. public void Calculate() { lineTotal = Decimal.Multiply(unitPrice, Convert.ToDecimal(quantity)); } //Calculate } //OrderedItem public class Test { public static void main(String[] args) { Test t = new Test(); // Write a purchase order. t.SerializeObject("simple.xml"); } //main private void SerializeObject(String fileName) { Console.WriteLine("Writing With XmlTextWriter"); XmlSerializer serializer = new XmlSerializer(OrderedItem.class.ToType()); OrderedItem i = new OrderedItem(); i.itemName = "Widget"; i.description = "Regular Widget"; i.quantity = 10; i.unitPrice = Convert.ToDecimal(2.3); i.Calculate(); // Create an XmlSerializerNamespaces object. XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); // Add two namespaces with prefixes. ns.Add("inventory", "http://www.cpandl.com"); ns.Add("money", "http://www.cohowinery.com"); // Create an XmlTextWriter using a FileStream. Stream fs = new FileStream(fileName, FileMode.Create); XmlWriter writer = new XmlTextWriter(fs, new UTF8Encoding()); // Serialize using the XmlTextWriter. serializer.Serialize(writer, i, ns); writer.Close(); } //SerializeObject } //Test
<?xml version="1.0"?> <OrderedItem xmlns:inventory="http://www.cpandl.com" xmlns:money="http://www.cohowinery.com"> <inventory:ItemName>Widget</inventory:ItemName> <inventory:Description>Regular Widget</inventory:Description> <money:UnitPrice>2.3</money:UnitPrice> <inventory:Quantity>10</inventory:Quantity> <money:LineTotal>23</money:LineTotal> </OrderedItem>

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


XmlSerializer.Serialize メソッド (Stream, Object)
アセンブリ: System.Xml (system.xml.dll 内)



Serialize メソッドは、オブジェクトのパブリック フィールドおよびパブリックな読み書き可能プロパティを XML に変換します。メソッド、インデクサ、プライベート フィールド、または読み取り専用プロパティは変換されません。オブジェクトのパブリックとプライベート両方のフィールドとプロパティをすべてシリアル化するには、BinaryFormatter を使用します。
stream パラメータには、抽象 Stream クラスから派生したオブジェクトを指定します。Stream の派生クラスには、次のクラスが含まれます。
-
BufferedStream
-
FileStream
-
MemoryStream
-
NetworkStream
-
CryptoStream
メモ 次のような ArrayList の配列および List の配列については、XmlSerializer でシリアル化することはできません。

Stream オブジェクトを使用して、オブジェクトをシリアル化する例を次に示します。
Imports System Imports System.IO Imports System.Xml.Serialization ' This is the class that will be serialized. Public Class OrderedItem Public ItemName As String Public Description As String Public UnitPrice As Decimal Public Quantity As Integer Public LineTotal As Decimal ' A custom method used to calculate price per item. Public Sub Calculate() LineTotal = UnitPrice * Quantity End Sub End Class Public Class Test Public Shared Sub Main() Dim t As New Test() ' Write a purchase order. t.SerializeObject("simple.xml") End Sub Private Sub SerializeObject(ByVal filename As String) Console.WriteLine("Writing With Stream") Dim serializer As New XmlSerializer(GetType(OrderedItem)) Dim i As New OrderedItem() With i .ItemName = "Widget" .Description = "Regular Widget" .Quantity = 10 .UnitPrice = CDec(2.3) .Calculate() End With ' Create a FileStream to write with. Dim writer As New FileStream(filename, FileMode.Create) ' Serialize the object, and close the TextWriter serializer.Serialize(writer, i) writer.Close() End Sub End Class
using System; using System.IO; using System.Xml.Serialization; // This is the class that will be serialized. public class OrderedItem { public string ItemName; public string Description; public decimal UnitPrice; public int Quantity; public decimal LineTotal; // A custom method used to calculate price per item. public void Calculate() { LineTotal = UnitPrice * Quantity; } } public class Test{ public static void Main(string[] args) { Test t = new Test(); // Write a purchase order. t.SerializeObject("simple.xml"); } private void SerializeObject(string filename) { Console.WriteLine("Writing With Stream"); XmlSerializer serializer = new XmlSerializer(typeof(OrderedItem)); OrderedItem i = new OrderedItem(); i.ItemName = "Widget"; i.Description = "Regular Widget"; i.Quantity = 10; i.UnitPrice = (decimal) 2.30; i.Calculate(); // Create a FileStream to write with. Stream writer = new FileStream(filename, FileMode.Create); // Serialize the object, and close the TextWriter serializer.Serialize(writer, i); writer.Close(); } }
#using <System.Xml.dll> #using <System.dll> using namespace System; using namespace System::IO; using namespace System::Xml::Serialization; // This is the class that will be serialized. public ref class OrderedItem { public: String^ ItemName; String^ Description; Decimal UnitPrice; int Quantity; Decimal LineTotal; // A custom method used to calculate price per item. void Calculate() { LineTotal = UnitPrice * Quantity; } }; void SerializeObject( String^ filename ) { Console::WriteLine( "Writing With Stream" ); XmlSerializer^ serializer = gcnew XmlSerializer( OrderedItem::typeid ); OrderedItem^ i = gcnew OrderedItem; i->ItemName = "Widget"; i->Description = "Regular Widget"; i->Quantity = 10; i->UnitPrice = (Decimal)2.30; i->Calculate(); // Create a FileStream to write with. Stream^ writer = gcnew FileStream( filename,FileMode::Create ); // Serialize the object, and close the TextWriter serializer->Serialize( writer, i ); writer->Close(); } int main() { // Write a purchase order. SerializeObject( "simple.xml" ); }
import System.*; import System.IO.*; import System.Xml.Serialization.*; // This is the class that will be serialized. public class OrderedItem { public String itemName; public String description; public System.Decimal unitPrice; public int quantity; public System.Decimal lineTotal; // A custom method used to calculate price per item. public void Calculate() { lineTotal = Decimal.Multiply(unitPrice, Convert.ToDecimal(quantity)); } //Calculate } //OrderedItem public class Test { public static void main(String[] args) { Test t = new Test(); // Write a purchase order. t.SerializeObject("simple.xml"); } //main private void SerializeObject(String fileName) { Console.WriteLine("Writing With Stream"); XmlSerializer serializer = new XmlSerializer(OrderedItem.class.ToType()); OrderedItem i = new OrderedItem(); i.itemName = "Widget"; i.description = "Regular Widget"; i.quantity = 10; i.unitPrice = Convert.ToDecimal(2.3); i.Calculate(); // Create a FileStream to write with. Stream writer = new FileStream(fileName, FileMode.Create); // Serialize the object, and close the TextWriter serializer.Serialize(writer, i); writer.Close(); } //SerializeObject } //Test
<?xml version="1.0"?> <OrderedItem xmlns:inventory="http://www.cpandl.com" xmlns:money="http://www.cohowinery.com"> <inventory:ItemName>Widget</inventory:ItemName> <inventory:Description>Regular Widget</inventory:Description> <money:UnitPrice>2.3</money:UnitPrice> <inventory:Quantity>10</inventory:Quantity> <money:LineTotal>23</money:LineTotal> </OrderedItem>

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


XmlSerializer.Serialize メソッド (TextWriter, Object)
アセンブリ: System.Xml (system.xml.dll 内)

Public Sub Serialize ( _ textWriter As TextWriter, _ o As Object _ )
Dim instance As XmlSerializer Dim textWriter As TextWriter Dim o As Object instance.Serialize(textWriter, o)
public void Serialize ( TextWriter textWriter, Object o )
public: void Serialize ( TextWriter^ textWriter, Object^ o )
public void Serialize ( TextWriter textWriter, Object o )
public function Serialize ( textWriter : TextWriter, o : Object )

Serialize メソッドは、オブジェクトのパブリック フィールドおよびパブリックな読み書き可能プロパティを XML に変換します。メソッド、インデクサ、プライベート フィールド、または読み取り専用プロパティは変換されません。オブジェクトのパブリックとプライベート両方のフィールドとプロパティをすべてシリアル化するには、BinaryFormatter を使用します。
textWriter パラメータには、抽象 TextWriter クラスから派生したオブジェクトを指定します。TextWriter の派生クラスには、次のクラスが含まれます。
-
StreamWriter
-
StringWriter
-
IndentedTextWriter
メモ 次のような ArrayList の配列および List の配列については、XmlSerializer でシリアル化することはできません。

TextWriter を使用して、オブジェクトをシリアル化する例を次に示します。
Imports System Imports System.IO Imports System.Text Imports System.Xml.Serialization ' This is the class that will be serialized. Public Class OrderedItem Public ItemName As String Public Description As String Public UnitPrice As Decimal Public Quantity As Integer Public LineTotal As Decimal 'A custom method used to calculate price per item. Public Sub Calculate() LineTotal = UnitPrice * Quantity End Sub End Class Public Class Test Public Shared Sub Main() Dim t As New Test() ' Write a purchase order. t.SerializeObject("simple.xml") End Sub Private Sub SerializeObject(ByVal filename As String) Console.WriteLine("Writing With TextWriter") Dim serializer As New XmlSerializer(GetType(OrderedItem)) Dim i As New OrderedItem() With i .ItemName = "Widget" .Description = "Regular Widget" .Quantity = 10 .UnitPrice = CDec(2.3) .Calculate() End With ' Create a StreamWriter to write with. First create a FileStream ' object, and create the StreamWriter specifying an Encoding to use. Dim fs As New FileStream(filename, FileMode.Create) Dim writer As New StreamWriter(fs, New UTF8Encoding()) ' Serialize using the XmlTextWriter. serializer.Serialize(writer, i) writer.Close() End Sub End Class
using System; using System.IO; using System.Text; using System.Xml.Serialization; // This is the class that will be serialized. public class OrderedItem { public string ItemName; public string Description; public decimal UnitPrice; public int Quantity; public decimal LineTotal; // A custom method used to calculate price per item. public void Calculate() { LineTotal = UnitPrice * Quantity; } } public class Test{ public static void Main(string[] args) { Test t = new Test(); // Write a purchase order. t.SerializeObject("simple.xml"); } private void SerializeObject(string filename) { Console.WriteLine("Writing With TextWriter"); XmlSerializer serializer = new XmlSerializer(typeof(OrderedItem)); OrderedItem i = new OrderedItem(); i.ItemName = "Widget"; i.Description = "Regular Widget"; i.Quantity = 10; i.UnitPrice = (decimal) 2.30; i.Calculate(); /* Create a StreamWriter to write with. First create a FileStream object, and create the StreamWriter specifying an Encoding to use. */ FileStream fs = new FileStream(filename, FileMode.Create); TextWriter writer = new StreamWriter(fs, new UTF8Encoding()); // Serialize using the XmlTextWriter. serializer.Serialize(writer, i); writer.Close(); } }
#using <System.Xml.dll> #using <System.dll> using namespace System; using namespace System::IO; using namespace System::Text; using namespace System::Xml::Serialization; // This is the class that will be serialized. public ref class OrderedItem { public: String^ ItemName; String^ Description; Decimal UnitPrice; int Quantity; Decimal LineTotal; // A custom method used to calculate price per item. void Calculate() { LineTotal = UnitPrice * Quantity; } }; void SerializeObject( String^ filename ) { Console::WriteLine( "Writing With TextWriter" ); XmlSerializer^ serializer = gcnew XmlSerializer( OrderedItem::typeid ); OrderedItem^ i = gcnew OrderedItem; i->ItemName = "Widget"; i->Description = "Regular Widget"; i->Quantity = 10; i->UnitPrice = (Decimal)2.30; i->Calculate(); /* Create a StreamWriter to write with. First create a FileStream object, and create the StreamWriter specifying an Encoding to use. */ FileStream^ fs = gcnew FileStream( filename,FileMode::Create ); TextWriter^ writer = gcnew StreamWriter( fs,gcnew UTF8Encoding ); // Serialize using the XmlTextWriter. serializer->Serialize( writer, i ); writer->Close(); } int main() { // Write a purchase order. SerializeObject( "simple.xml" ); }
import System.*; import System.IO.*; import System.Text.*; import System.Xml.Serialization.*; // This is the class that will be serialized. public class OrderedItem { public String itemName; public String description; public System.Decimal unitPrice; public int quantity; public System.Decimal lineTotal; // A custom method used to calculate price per item. public void Calculate() { lineTotal = Decimal.Multiply(unitPrice, Convert.ToDecimal(quantity)); } //Calculate } //OrderedItem public class Test { public static void main(String[] args) { Test t = new Test(); // Write a purchase order. t.SerializeObject("simple.xml"); } //main private void SerializeObject(String fileName) { Console.WriteLine("Writing With TextWriter"); XmlSerializer serializer = new XmlSerializer(OrderedItem.class.ToType()); OrderedItem i = new OrderedItem(); i.itemName = "Widget"; i.description = "Regular Widget"; i.quantity = 10; i.unitPrice = Convert.ToDecimal(2.3); i.Calculate(); /* Create a StreamWriter to write with. First create a FileStream object, and create the StreamWriter specifying an Encoding to use. */ FileStream fs = new FileStream(fileName, FileMode.Create); TextWriter writer = new StreamWriter(fs, new UTF8Encoding()); // Serialize using the XmlTextWriter. serializer.Serialize(writer, i); writer.Close(); } //SerializeObject } //Test
<?xml version="1.0"?> <OrderedItem xmlns:inventory="http://www.cpandl.com" xmlns:money="http://www.cohowinery.com"> <inventory:ItemName>Widget</inventory:ItemName> <inventory:Description>Regular Widget</inventory:Description> <money:UnitPrice>2.3</money:UnitPrice> <inventory:Quantity>10</inventory:Quantity> <money:LineTotal>23</money:LineTotal> </OrderedItem>

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


XmlSerializer.Serialize メソッド (Stream, Object, XmlSerializerNamespaces)
アセンブリ: System.Xml (system.xml.dll 内)

Public Sub Serialize ( _ stream As Stream, _ o As Object, _ namespaces As XmlSerializerNamespaces _ )
Dim instance As XmlSerializer Dim stream As Stream Dim o As Object Dim namespaces As XmlSerializerNamespaces instance.Serialize(stream, o, namespaces)


Serialize メソッドを呼び出すと、オブジェクトのパブリック フィールドとパブリックな読み書き可能プロパティが XML に変換されます。メソッド、インデクサ、プライベート フィールド、および読み取り専用プロパティはシリアル化されません。パブリックとプライベート両方のフィールドとプロパティをすべてシリアル化するには、BinaryFormatter を使用します。
stream パラメータを使用して、ストリームに書き込むようにデザインされている、抽象 Stream クラスから派生したオブジェクトを指定します。Stream の派生クラスには、次のクラスが含まれます。
-
BufferedStream
-
FileStream
-
MemoryStream
-
NetworkStream
-
CryptoStream
メモ 次のような ArrayList の配列および List の配列については、XmlSerializer でシリアル化することはできません。

Stream オブジェクトを使用して、オブジェクトをシリアル化する例を次に示します。この例では、XmlSerializerNamespaces も作成し、そのオブジェクトに 2 つの名前空間を追加します。シリアル化されたオブジェクトを定義するクラスには、各要素の名前空間を指定する XmlElementAttribute 属性も設定されます。
Imports System Imports System.IO Imports System.Xml.Serialization Imports Microsoft.VisualBasic ' This is the class that will be serialized. Public Class OrderedItem <XmlElement(Namespace := "http://www.cpandl.com")> _ Public ItemName As String <XmlElement(Namespace := "http://www.cpandl.com")> _ Public Description As String <XmlElement(Namespace := "http://www.cohowinery.com")> _ Public UnitPrice As Decimal <XmlElement(Namespace := "http://www.cpandl.com")> _ Public Quantity As Integer <XmlElement(Namespace := "http://www.cohowinery.com")> _ Public LineTotal As Decimal ' A custom method used to calculate price per item. Public Sub Calculate() LineTotal = UnitPrice * Quantity End Sub End Class Public Class Test Public Shared Sub Main() Dim t As New Test() ' Write a purchase order. t.SerializeObject("simple.xml") t.DeserializeObject("simple.xml") End Sub Private Sub SerializeObject(ByVal filename As String) Console.WriteLine("Writing With Stream") Dim serializer As New XmlSerializer(GetType(OrderedItem)) Dim i As New OrderedItem() With i .ItemName = "Widget" .Description = "Regular Widget" .Quantity = 10 .UnitPrice = CDec(2.3) .Calculate() End With ' Create an XmlSerializerNamespaces object. Dim ns As New XmlSerializerNamespaces() ' Add two prefix-namespace pairs. ns.Add("inventory", "http://www.cpandl.com") ns.Add("money", "http://www.cohowinery.com") ' Create a FileStream to write with. Dim writer As New FileStream(filename, FileMode.Create) ' Serialize the object, and close the TextWriter serializer.Serialize(writer, i, ns) writer.Close() End Sub Private Sub DeserializeObject(ByVal filename As String) Console.WriteLine("Reading with Stream") ' Create an instance of the XmlSerializer. Dim serializer As New XmlSerializer(GetType(OrderedItem)) ' Writing the file requires a Stream. Dim reader As New FileStream(filename, FileMode.Open) ' Declare an object variable of the type to be deserialized. Dim i As OrderedItem ' Use the Deserialize method to restore the object's state ' using data from the XML document. i = CType(serializer.Deserialize(reader), OrderedItem) ' Write out the properties of the object. Console.Write(i.ItemName & ControlChars.Tab & _ i.Description & ControlChars.Tab & _ i.UnitPrice & ControlChars.Tab & _ i.Quantity & ControlChars.Tab & _ i.LineTotal) End Sub End Class
using System; using System.IO; using System.Xml.Serialization; // This is the class that will be serialized. public class OrderedItem { [XmlElement(Namespace = "http://www.cpandl.com")] public string ItemName; [XmlElement(Namespace = "http://www.cpandl.com")] public string Description; [XmlElement(Namespace="http://www.cohowinery.com")] public decimal UnitPrice; [XmlElement(Namespace = "http://www.cpandl.com")] public int Quantity; [XmlElement(Namespace="http://www.cohowinery.com")] public decimal LineTotal; // A custom method used to calculate price per item. public void Calculate() { LineTotal = UnitPrice * Quantity; } } public class Test { public static void Main() { Test t = new Test(); // Write a purchase order. t.SerializeObject("simple.xml"); t.DeserializeObject("simple.xml"); } private void SerializeObject(string filename) { Console.WriteLine("Writing With Stream"); XmlSerializer serializer = new XmlSerializer(typeof(OrderedItem)); OrderedItem i = new OrderedItem(); i.ItemName = "Widget"; i.Description = "Regular Widget"; i.Quantity = 10; i.UnitPrice = (decimal) 2.30; i.Calculate(); // Create an XmlSerializerNamespaces object. XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); // Add two prefix-namespace pairs. ns.Add("inventory", "http://www.cpandl.com"); ns.Add("money", "http://www.cohowinery.com"); // Create a FileStream to write with. Stream writer = new FileStream(filename, FileMode.Create); // Serialize the object, and close the TextWriter serializer.Serialize(writer, i, ns); writer.Close(); } private void DeserializeObject(string filename) { Console.WriteLine("Reading with Stream"); // Create an instance of the XmlSerializer. XmlSerializer serializer = new XmlSerializer(typeof(OrderedItem)); // Writing the file requires a Stream. Stream reader= new FileStream(filename,FileMode.Open); // Declare an object variable of the type to be deserialized. OrderedItem i; /* Use the Deserialize method to restore the object's state using data from the XML document. */ i = (OrderedItem) serializer.Deserialize(reader); // Write out the properties of the object. Console.Write( i.ItemName + "\t" + i.Description + "\t" + i.UnitPrice + "\t" + i.Quantity + "\t" + i.LineTotal); } }
#using <System.Xml.dll> #using <System.dll> using namespace System; using namespace System::IO; using namespace System::Xml::Serialization; // This is the class that will be serialized. public ref class OrderedItem { public: [XmlElement(Namespace="http://www.cpandl.com")] String^ ItemName; [XmlElement(Namespace="http://www.cpandl.com")] String^ Description; [XmlElement(Namespace="http://www.cohowinery.com")] Decimal UnitPrice; [XmlElement(Namespace="http://www.cpandl.com")] int Quantity; [XmlElement(Namespace="http://www.cohowinery.com")] Decimal LineTotal; // A custom method used to calculate price per item. void Calculate() { LineTotal = UnitPrice * Quantity; } }; void SerializeObject( String^ filename ) { Console::WriteLine( "Writing With Stream" ); XmlSerializer^ serializer = gcnew XmlSerializer( OrderedItem::typeid ); OrderedItem^ i = gcnew OrderedItem; i->ItemName = "Widget"; i->Description = "Regular Widget"; i->Quantity = 10; i->UnitPrice = (Decimal)2.30; i->Calculate(); // Create an XmlSerializerNamespaces object. XmlSerializerNamespaces^ ns = gcnew XmlSerializerNamespaces; // Add two prefix-namespace pairs. ns->Add( "inventory", "http://www.cpandl.com" ); ns->Add( "money", "http://www.cohowinery.com" ); // Create a FileStream to write with. Stream^ writer = gcnew FileStream( filename,FileMode::Create ); // Serialize the object, and close the TextWriter serializer->Serialize( writer, i, ns ); writer->Close(); } void DeserializeObject( String^ filename ) { Console::WriteLine( "Reading with Stream" ); // Create an instance of the XmlSerializer. XmlSerializer^ serializer = gcnew XmlSerializer( OrderedItem::typeid ); // Writing the file requires a Stream. Stream^ reader = gcnew FileStream( filename,FileMode::Open ); // Declare an object variable of the type to be deserialized. OrderedItem^ i; /* Use the Deserialize method to restore the object's state using data from the XML document. */ i = dynamic_cast<OrderedItem^>(serializer->Deserialize( reader )); // Write out the properties of the object. Console::Write( "{0}\t{1}\t{2}\t{3}\t{4}", i->ItemName, i->Description, i->UnitPrice, i->Quantity, i->LineTotal ); } int main() { // Write a purchase order. SerializeObject( "simple.xml" ); DeserializeObject( "simple.xml" ); }
import System.*; import System.IO.*; import System.Xml.Serialization.*; // This is the class that will be serialized. public class OrderedItem { /** @attribute XmlElement(Namespace = "http://www.cpandl.com") */ public String itemName; /** @attribute XmlElement(Namespace = "http://www.cpandl.com") */ public String description; /** @attribute XmlElement(Namespace = "http://www.cohowinery.com") */ public System.Decimal unitPrice; /** @attribute XmlElement(Namespace = "http://www.cpandl.com") */ public int quantity; /** @attribute XmlElement(Namespace = "http://www.cohowinery.com") */ public System.Decimal lineTotal; // A custom method used to calculate price per item. public void Calculate() { lineTotal = Decimal.Multiply(unitPrice, Convert.ToDecimal(quantity)); } //Calculate } //OrderedItem public class Test { public static void main(String[] args) { Test t = new Test(); // Write a purchase order. t.SerializeObject("simple.xml"); t.DeserializeObject("simple.xml"); } //main private void SerializeObject(String fileName) { Console.WriteLine("Writing With Stream"); XmlSerializer serializer = new XmlSerializer(OrderedItem.class.ToType()); OrderedItem i = new OrderedItem(); i.itemName = "Widget"; i.description = "Regular Widget"; i.quantity = 10; i.unitPrice = Convert.ToDecimal(2.3); i.Calculate(); // Create an XmlSerializerNamespaces object. XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); // Add two prefix-namespace pairs. ns.Add("inventory", "http://www.cpandl.com"); ns.Add("money", "http://www.cohowinery.com"); // Create a FileStream to write with. Stream writer = new FileStream(fileName, FileMode.Create); // Serialize the object, and close the TextWriter serializer.Serialize(writer, i, ns); writer.Close(); } //SerializeObject private void DeserializeObject(String fileName) { Console.WriteLine("Reading with Stream"); // Create an instance of the XmlSerializer. XmlSerializer serializer = new XmlSerializer(OrderedItem.class.ToType()); // Writing the file requires a Stream. Stream reader = new FileStream(fileName, FileMode.Open); // Declare an object variable of the type to be deserialized. OrderedItem i; /* Use the Deserialize method to restore the object's state using data from the XML document. */ i = (OrderedItem)serializer.Deserialize(reader); // Write out the properties of the object. Console.Write(i.itemName + "\t" + i.description + "\t" + i.unitPrice + "\t" + i.quantity + "\t" + i.lineTotal); } //DeserializeObject } //Test

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


XmlSerializer.Serialize メソッド (TextWriter, Object, XmlSerializerNamespaces)
アセンブリ: System.Xml (system.xml.dll 内)

Public Sub Serialize ( _ textWriter As TextWriter, _ o As Object, _ namespaces As XmlSerializerNamespaces _ )
Dim instance As XmlSerializer Dim textWriter As TextWriter Dim o As Object Dim namespaces As XmlSerializerNamespaces instance.Serialize(textWriter, o, namespaces)
public void Serialize ( TextWriter textWriter, Object o, XmlSerializerNamespaces namespaces )
public: void Serialize ( TextWriter^ textWriter, Object^ o, XmlSerializerNamespaces^ namespaces )
public void Serialize ( TextWriter textWriter, Object o, XmlSerializerNamespaces namespaces )
public function Serialize ( textWriter : TextWriter, o : Object, namespaces : XmlSerializerNamespaces )


Serialize メソッドを呼び出すと、オブジェクトのパブリック フィールドとパブリックな読み書き可能プロパティが XML に変換されます。メソッド、インデクサ、プライベート フィールド、および読み取り専用プロパティはシリアル化されません。パブリックとプライベート両方のフィールドとプロパティをすべてシリアル化するには、BinaryFormatter を使用します。
textWriter パラメータを使用して、抽象 TextWriter クラスから派生したオブジェクトを指定します。TextWriter クラスの派生クラスには、次のクラスが含まれます。
-
StreamWriter
-
StringWriter
-
IndentedTextWriter
メモ 次のような ArrayList の配列および List の配列については、XmlSerializer でシリアル化することはできません。

TextWriter オブジェクトを使用して、オブジェクトをシリアル化する例を次に示します。この例では、XmlSerializerNamespaces オブジェクトも作成し、そのオブジェクトに 2 つの名前空間を追加します。シリアル化されたオブジェクトを定義するクラスには、各要素の名前空間を指定する XmlElementAttribute 属性も設定されます。
Imports System Imports System.IO Imports System.Xml.Serialization ' This is the class that will be serialized. Public Class OrderedItem <XmlElement(Namespace := "http://www.cpandl.com")> _ Public ItemName As String <XmlElement(Namespace := "http://www.cpandl.com")> _ Public Description As String <XmlElement(Namespace := "http://www.cohowinery.com")> _ Public UnitPrice As Decimal <XmlElement(Namespace := "http://www.cpandl.com")> _ Public Quantity As Integer <XmlElement(Namespace := "http://www.cohowinery.com")> _ Public LineTotal As Decimal 'A custom method used to calculate price per item. Public Sub Calculate() LineTotal = UnitPrice * Quantity End Sub End Class Public Class Test Public Shared Sub Main() Dim t As New Test() ' Write a purchase order. t.SerializeObject("simple.xml") End Sub Private Sub SerializeObject(ByVal filename As String) Console.WriteLine("Writing With TextWriter") ' Create an XmlSerializer instance using the type. Dim serializer As New XmlSerializer(GetType(OrderedItem)) Dim i As New OrderedItem() i.ItemName = "Widget" i.Description = "Regular Widget" i.Quantity = 10 i.UnitPrice = CDec(2.3) i.Calculate() ' Create an XmlSerializerNamespaces object. Dim ns As New XmlSerializerNamespaces() ' Add two namespaces with prefixes. ns.Add("inventory", "http://www.cpandl.com") ns.Add("money", "http://www.cohowinery.com") ' Create a StreamWriter to write with. Dim writer As New StreamWriter(filename) ' Serialize using the object using the TextWriter ' and namespaces. serializer.Serialize(writer, i, ns) writer.Close() End Sub End Class
using System; using System.IO; using System.Xml.Serialization; // This is the class that will be serialized. public class OrderedItem { [XmlElement(Namespace = "http://www.cpandl.com")] public string ItemName; [XmlElement(Namespace = "http://www.cpandl.com")] public string Description; [XmlElement(Namespace="http://www.cohowinery.com")] public decimal UnitPrice; [XmlElement(Namespace = "http://www.cpandl.com")] public int Quantity; [XmlElement(Namespace="http://www.cohowinery.com")] public decimal LineTotal; // A custom method used to calculate price per item. public void Calculate() { LineTotal = UnitPrice * Quantity; } } public class Test{ public static void Main(string[] args) { Test t = new Test(); // Write a purchase order. t.SerializeObject("simple.xml"); } private void SerializeObject(string filename) { Console.WriteLine("Writing With TextWriter"); // Create an XmlSerializer instance using the type. XmlSerializer serializer = new XmlSerializer(typeof(OrderedItem)); OrderedItem i = new OrderedItem(); i.ItemName = "Widget"; i.Description = "Regular Widget"; i.Quantity = 10; i.UnitPrice = (decimal) 2.30; i.Calculate(); // Create an XmlSerializerNamespaces object. XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); // Add two namespaces with prefixes. ns.Add("inventory", "http://www.cpandl.com"); ns.Add("money", "http://www.cohowinery.com"); // Create a StreamWriter to write with. TextWriter writer = new StreamWriter(filename); /* Serialize using the object using the TextWriter and namespaces. */ serializer.Serialize(writer, i, ns); writer.Close(); } }
#using <System.Xml.dll> #using <System.dll> using namespace System; using namespace System::IO; using namespace System::Xml::Serialization; // This is the class that will be serialized. public ref class OrderedItem { public: [XmlElement(Namespace="http://www.cpandl.com")] String^ ItemName; [XmlElement(Namespace="http://www.cpandl.com")] String^ Description; [XmlElement(Namespace="http://www.cohowinery.com")] Decimal UnitPrice; [XmlElement(Namespace="http://www.cpandl.com")] int Quantity; [XmlElement(Namespace="http://www.cohowinery.com")] Decimal LineTotal; // A custom method used to calculate price per item. void Calculate() { LineTotal = UnitPrice * Quantity; } }; void SerializeObject( String^ filename ) { Console::WriteLine( "Writing With TextWriter" ); // Create an XmlSerializer instance using the type. XmlSerializer^ serializer = gcnew XmlSerializer( OrderedItem::typeid ); OrderedItem^ i = gcnew OrderedItem; i->ItemName = "Widget"; i->Description = "Regular Widget"; i->Quantity = 10; i->UnitPrice = (Decimal)2.30; i->Calculate(); // Create an XmlSerializerNamespaces object. XmlSerializerNamespaces^ ns = gcnew XmlSerializerNamespaces; // Add two namespaces with prefixes. ns->Add( "inventory", "http://www.cpandl.com" ); ns->Add( "money", "http://www.cohowinery.com" ); // Create a StreamWriter to write with. TextWriter^ writer = gcnew StreamWriter( filename ); /* Serialize using the object using the TextWriter and namespaces. */ serializer->Serialize( writer, i, ns ); writer->Close(); } int main() { // Write a purchase order. SerializeObject( "simple.xml" ); }
import System.*; import System.IO.*; import System.Xml.Serialization.*; // This is the class that will be serialized. public class OrderedItem { /** @attribute XmlElement(Namespace = "http://www.cpandl.com") */ public String itemName; /** @attribute XmlElement(Namespace = "http://www.cpandl.com") */ public String description; /** @attribute XmlElement(Namespace = "http://www.cohowinery.com") */ public System.Decimal unitPrice; /** @attribute XmlElement(Namespace = "http://www.cpandl.com") */ public int quantity; /** @attribute XmlElement(Namespace = "http://www.cohowinery.com") */ public System.Decimal lineTotal; // A custom method used to calculate price per item. public void Calculate() { lineTotal = Decimal.Multiply(unitPrice, Convert.ToDecimal(quantity)); } //Calculate } //OrderedItem public class Test { public static void main(String[] args) { Test t = new Test(); // Write a purchase order. t.SerializeObject("simple.xml"); } //main private void SerializeObject(String fileName) { Console.WriteLine("Writing With TextWriter"); // Create an XmlSerializer instance using the type. XmlSerializer serializer = new XmlSerializer(OrderedItem.class.ToType()); OrderedItem i = new OrderedItem(); i.itemName = "Widget"; i.description = "Regular Widget"; i.quantity = 10; i.unitPrice = Convert.ToDecimal(2.3); i.Calculate(); // Create an XmlSerializerNamespaces object. XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); // Add two namespaces with prefixes. ns.Add("inventory", "http://www.cpandl.com"); ns.Add("money", "http://www.cohowinery.com"); // Create a StreamWriter to write with. TextWriter writer = new StreamWriter(fileName); /* Serialize using the object using the TextWriter and namespaces. */ serializer.Serialize(writer, i, ns); writer.Close(); } //SerializeObject } //Test
<?xml version="1.0"?> <OrderedItem xmlns:inventory="http://www.cpandl.com" xmlns:money="http://www.cohowinery.com"> <inventory:ItemName>Widget</inventory:ItemName> <inventory:Description>Regular Widget</inventory:Description> <money:UnitPrice>2.3</money:UnitPrice> <inventory:Quantity>10</inventory:Quantity> <money:LineTotal>23</money:LineTotal> </OrderedItem>

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


XmlSerializer.Serialize メソッド (XmlWriter, Object, XmlSerializerNamespaces, String, String)
アセンブリ: System.Xml (system.xml.dll 内)

Public Sub Serialize ( _ xmlWriter As XmlWriter, _ o As Object, _ namespaces As XmlSerializerNamespaces, _ encodingStyle As String, _ id As String _ )
Dim instance As XmlSerializer Dim xmlWriter As XmlWriter Dim o As Object Dim namespaces As XmlSerializerNamespaces Dim encodingStyle As String Dim id As String instance.Serialize(xmlWriter, o, namespaces, encodingStyle, id)
public void Serialize ( XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, string encodingStyle, string id )
public: void Serialize ( XmlWriter^ xmlWriter, Object^ o, XmlSerializerNamespaces^ namespaces, String^ encodingStyle, String^ id )
public void Serialize ( XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id )
public function Serialize ( xmlWriter : XmlWriter, o : Object, namespaces : XmlSerializerNamespaces, encodingStyle : String, id : String )

id パラメータは、SOAP id の作成に使用する基本文字列となります。既定では、id は、"id1"、"id2" などになります。しかし、パラメータが "myBase" に設定されている場合は、生成される値は "myBaseid1"、"myBaseid2" などになります。この機能は、SOAP メッセージ全体の中で一意の id 値を作成するために使用されます。

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


XmlSerializer.Serialize メソッド


XmlSerializer.Serialize メソッド (XmlWriter, Object, XmlSerializerNamespaces, String)
アセンブリ: System.Xml (system.xml.dll 内)

Public Sub Serialize ( _ xmlWriter As XmlWriter, _ o As Object, _ namespaces As XmlSerializerNamespaces, _ encodingStyle As String _ )
Dim instance As XmlSerializer Dim xmlWriter As XmlWriter Dim o As Object Dim namespaces As XmlSerializerNamespaces Dim encodingStyle As String instance.Serialize(xmlWriter, o, namespaces, encodingStyle)
public void Serialize ( XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, string encodingStyle )
public: void Serialize ( XmlWriter^ xmlWriter, Object^ o, XmlSerializerNamespaces^ namespaces, String^ encodingStyle )
public void Serialize ( XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle )
public function Serialize ( xmlWriter : XmlWriter, o : Object, namespaces : XmlSerializerNamespaces, encodingStyle : String )


Serialize メソッドを呼び出すと、オブジェクトのパブリック フィールドとパブリックな読み書き可能プロパティが XML に変換されます。メソッド、インデクサ、プライベート フィールド、および読み取り専用プロパティはシリアル化されません。パブリックとプライベート両方のフィールドとプロパティをすべてシリアル化するには、BinaryFormatter を使用します。
xmlWriter パラメータを使用して、XML ドキュメントに書き込むようにデザインされている、抽象 XmlWriter クラスから派生したオブジェクトを指定します。XmlTextWriter は XmlWriter から派生します。
SOAP Version 1.1 エンコーディングを使用する場合は、encodingStyle パラメータを "http://schemas.xmlsoap.org/soap/encoding/" に設定します。それ以外の場合は、SOAP Version 1.2 エンコーディングを使用する "http://www.w3.org/2001/12/soap-encoding" に設定します。
メモ 次のような ArrayList の配列および List の配列については、XmlSerializer でシリアル化することはできません。

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- XmlSerializer.Serialize メソッドのページへのリンク