XmlSerializer.Serializeとは? わかりやすく解説

XmlSerializer.Serialize メソッド (XmlWriter, Object)

指定した Object をシリアル化し、生成された XML ドキュメントを、指定した XmlWriter を使用してファイルに書き込みます。

名前空間: System.Xml.Serialization
アセンブリ: System.Xml (system.xml.dll 内)
構文構文

例外例外
解説解説
使用例使用例

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>

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

XmlSerializer.Serialize メソッド (Object, XmlSerializationWriter)

指定した Object をシリアル化し、生成された XML ドキュメントを、指定した XmlSerializationWriter を使用してファイルに書き込みます。

名前空間: System.Xml.Serialization
アセンブリ: System.Xml (system.xml.dll 内)
構文構文

Protected Overridable Sub
 Serialize ( _
    o As Object, _
    writer As XmlSerializationWriter _
)
Dim o As Object
Dim writer As XmlSerializationWriter

Me.Serialize(o, writer)
protected virtual void Serialize (
    Object o,
    XmlSerializationWriter writer
)
protected:
virtual void Serialize (
    Object^ o, 
    XmlSerializationWriter^ writer
)
protected void Serialize (
    Object o, 
    XmlSerializationWriter writer
)
protected function Serialize (
    o : Object, 
    writer : XmlSerializationWriter
)

パラメータ

o

シリアル化する Object。

writer

XML ドキュメントを書き込むために使用する XmlSerializationWriter。

例外例外
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
XmlSerializer クラス
XmlSerializer メンバ
System.Xml.Serialization 名前空間

XmlSerializer.Serialize メソッド (XmlWriter, Object, XmlSerializerNamespaces)

指定した Object をシリアル化し、指定した XmlWriter を使用して、指定した名前空間を参照し、生成された XML ドキュメントをファイルに書き込みます。

名前空間: System.Xml.Serialization
アセンブリ: 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 void Serialize (
    XmlWriter xmlWriter,
    Object o,
    XmlSerializerNamespaces namespaces
)
public:
void Serialize (
    XmlWriter^ xmlWriter, 
    Object^ o, 
    XmlSerializerNamespaces^ namespaces
)
public void Serialize (
    XmlWriter xmlWriter, 
    Object o, 
    XmlSerializerNamespaces namespaces
)
public function Serialize (
    xmlWriter : XmlWriter, 
    o : Object, 
    namespaces : XmlSerializerNamespaces
)

パラメータ

xmlWriter

XML ドキュメントを書き込むために使用する XmlWriter。

o

シリアル化する Object。

namespaces

オブジェクトが参照する XmlSerializerNamespaces。

例外例外
解説解説
使用例使用例

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>

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

XmlSerializer.Serialize メソッド (Stream, Object)

指定した Object をシリアル化し、生成された XML ドキュメントを、指定した Stream を使用してファイルに書き込みます。

名前空間: System.Xml.Serialization
アセンブリ: System.Xml (system.xml.dll 内)
構文構文

例外例外
解説解説
使用例使用例

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>

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

XmlSerializer.Serialize メソッド (TextWriter, Object)

指定した Object をシリアル化し、生成された XML ドキュメントを、指定した TextWriter を使用してファイルに書き込みます。

名前空間: System.Xml.Serialization
アセンブリ: System.Xml (system.xml.dll 内)
構文構文

解説解説
使用例使用例

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>

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

XmlSerializer.Serialize メソッド (Stream, Object, XmlSerializerNamespaces)

指定した Object をシリアル化し、指定した Stream を使用して、指定した名前空間を参照し、生成された XML ドキュメントをファイルに書き込みます。

名前空間: System.Xml.Serialization
アセンブリ: 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)
public void Serialize (
    Stream stream,
    Object o,
    XmlSerializerNamespaces namespaces
)
public:
void Serialize (
    Stream^ stream, 
    Object^ o, 
    XmlSerializerNamespaces^ namespaces
)
public void Serialize (
    Stream stream, 
    Object o, 
    XmlSerializerNamespaces namespaces
)
public function Serialize (
    stream : Stream, 
    o : Object, 
    namespaces : XmlSerializerNamespaces
)

パラメータ

stream

XML ドキュメントを書き込むために使用する Stream。

o

シリアル化する Object。

namespaces

オブジェクトが参照する XmlSerializerNamespaces。

例外例外
解説解説
使用例使用例

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
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

XmlSerializer.Serialize メソッド (TextWriter, Object, XmlSerializerNamespaces)

指定した Object をシリアル化し、指定した TextWriter を使用して、指定した名前空間を参照し、生成された XML ドキュメントをファイルに書き込みます。

名前空間: System.Xml.Serialization
アセンブリ: 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
)

パラメータ

textWriter

XML ドキュメントを書き込むために使用する TextWriter。

o

シリアル化する Object。

namespaces

生成された XML ドキュメントで使用する名前空間を格納している XmlSerializerNamespaces。

例外例外
解説解説
使用例使用例

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>

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

XmlSerializer.Serialize メソッド (XmlWriter, Object, XmlSerializerNamespaces, String, String)

メモ : このメソッドは、.NET Framework version 2.0 で新しく追加されたものです。

指定した Object をシリアル化し、指定した XmlWriter、XML 名前空間、およびエンコーディングを使用して、XML ドキュメントをファイルに書き込みます。

名前空間: System.Xml.Serialization
アセンブリ: 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
)

パラメータ

xmlWriter

XML ドキュメントを書き込むために使用する XmlWriter。

o

シリアル化するオブジェクト。

namespaces

使用する名前空間とプレフィックスを格納している XmlSerializaerNamespaces のインスタンス。

encodingStyle

ドキュメントで使用するエンコーディング。

id

SOAP エンコード済みメッセージの場合に、id 属性の生成に使用される基本文字列。

解説解説

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

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
XmlSerializer クラス
XmlSerializer メンバ
System.Xml.Serialization 名前空間

XmlSerializer.Serialize メソッド

オブジェクトを XML ドキュメントにシリアル化します。
オーバーロードの一覧オーバーロードの一覧

名前 説明
XmlSerializer.Serialize (Object, XmlSerializationWriter) 指定した Object をシリアル化し、生成された XML ドキュメントを、指定した XmlSerializationWriter を使用してファイルに書き込みます。
XmlSerializer.Serialize (Stream, Object) 指定した Object をシリアル化し、生成された XML ドキュメントを、指定した Stream を使用してファイルに書き込みます。

.NET Compact Framework によってサポートされています。

XmlSerializer.Serialize (TextWriter, Object) 指定した Object をシリアル化し、生成された XML ドキュメントを、指定した TextWriter を使用してファイルに書き込みます。

.NET Compact Framework によってサポートされています。

XmlSerializer.Serialize (XmlWriter, Object) 指定した Object をシリアル化し、生成された XML ドキュメントを、指定した XmlWriter を使用してファイルに書き込みます。

.NET Compact Framework によってサポートされています。

XmlSerializer.Serialize (Stream, Object, XmlSerializerNamespaces) 指定した Object をシリアル化し、指定した Stream を使用して、指定した名前空間を参照し、生成された XML ドキュメントをファイルに書き込みます。

.NET Compact Framework によってサポートされています。

XmlSerializer.Serialize (TextWriter, Object, XmlSerializerNamespaces) 指定した Object をシリアル化し、指定した TextWriter を使用して、指定した名前空間を参照し、生成された XML ドキュメントをファイルに書き込みます。

.NET Compact Framework によってサポートされています。

XmlSerializer.Serialize (XmlWriter, Object, XmlSerializerNamespaces) 指定した Object をシリアル化し、指定した XmlWriter を使用して、指定した名前空間を参照し、生成された XML ドキュメントをファイルに書き込みます。

.NET Compact Framework によってサポートされています。

XmlSerializer.Serialize (XmlWriter, Object, XmlSerializerNamespaces, String) 指定したオブジェクトをシリアル化し、指定した XmlWriter を使用して、指定した名前空間とエンコーディング スタイルを参照し、生成された XML ドキュメントをファイルに書き込みます。

.NET Compact Framework によってサポートされています。

XmlSerializer.Serialize (XmlWriter, Object, XmlSerializerNamespaces, String, String) 指定した Object をシリアル化し、指定した XmlWriter、XML 名前空間、およびエンコーディングを使用して、XML ドキュメントをファイルに書き込みます。
参照参照

関連項目

XmlSerializer クラス
XmlSerializer メンバ
System.Xml.Serialization 名前空間

XmlSerializer.Serialize メソッド (XmlWriter, Object, XmlSerializerNamespaces, String)

メモ : このメソッドは、.NET Framework version 2.0 で新しく追加されたものです。

指定したオブジェクトをシリアル化し、指定した XmlWriter を使用して、指定した名前空間とエンコーディング スタイルを参照し、生成された XML ドキュメントをファイルに書き込みます。

名前空間: System.Xml.Serialization
アセンブリ: 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
)

パラメータ

xmlWriter

XML ドキュメントを書き込むために使用する XmlWriter。

o

シリアル化するオブジェクト。

namespaces

オブジェクトが参照する XmlSerializerNamespaces。

encodingStyle

シリアル化された XML のエンコーディング スタイル。

例外例外
解説解説
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「XmlSerializer.Serialize」の関連用語

XmlSerializer.Serializeのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



XmlSerializer.Serializeのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS