次の方法で共有


XmlRootAttribute コンストラクタ ()

XmlRootAttribute クラスの新しいインスタンスを初期化します。

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

構文

'宣言
Public Sub New
'使用
Dim instance As New XmlRootAttribute
public XmlRootAttribute ()
public:
XmlRootAttribute ()
public XmlRootAttribute ()
public function XmlRootAttribute ()

使用例

この例では、XmlRootAttribute のインスタンスを作成し、そのインスタンスを XmlAttributes オブジェクトの XmlRoot プロパティに割り当てています。XmlSerializer は、MyClass オブジェクトをシリアル化するときに、XmlRootAttribute オブジェクトを使用して既定のルート要素をオーバーライドします。

Imports System
Imports System.IO
Imports System.Xml.Serialization


' This is the class that is the default root element.
Public Class MyClass1
    Public Name As String
End Class

Public Class Run
    
    Public Shared Sub Main()
        Dim test As New Run()
        test.SerializeOrder("OverrideAttribute.xml")
    End Sub
        
    Public Sub SerializeOrder(ByVal filename As String)
        ' Create an XmlSerializer instance using the method below.
        Dim xSer As XmlSerializer = CreateOverrider()
        
        ' Create the object, and set its Name property.
        Dim class1 As New MyClass1()
        class1.Name = "New Class Name"
        
        ' Serialize the class, and close the TextWriter.
        Dim writer As New StreamWriter(filename)
        xSer.Serialize(writer, class1)
        writer.Close()
    End Sub 'SerializeOrder
    
    
    ' Return an XmlSerializer to override the root serialization.
    Public Function CreateOverrider() As XmlSerializer
        ' Create an XmlAttributes to override the default root element.
        Dim attrs As New XmlAttributes()
        
        ' Create an XmlRootAttribute and set its element name and namespace.
        Dim xRoot As New XmlRootAttribute()
        xRoot.ElementName = "OverriddenRootElementName"
        xRoot.Namespace = "https://www.microsoft.com"
        
        ' Set the XmlRoot property to the XmlRoot object.
        attrs.XmlRoot = xRoot
        Dim xOver As New XmlAttributeOverrides()
        
        ' Add the XmlAttributes object to the
        ' XmlAttributeOverrides object. 
        xOver.Add(GetType(MyClass1), attrs)
        
        ' Create the Serializer, and return it.
        Dim xSer As New XmlSerializer(GetType(MyClass1), xOver)
        Return xSer
    End Function
End Class
using System;
using System.IO;
using System.Xml.Serialization;

// This is the class that is the default root element.
public class MyClass
{
   public string Name;
}

public class Run 
{
   public static void Main()
   {
      Run test = new Run();
      test.SerializeOrder("OverrideAttribute.xml");
   }

   public void SerializeOrder(string filename)
   {
      // Create an XmlSerializer instance using the method below.
      XmlSerializer xSer = CreateOverrider();

      // Create the object, and set its Name property.
      MyClass myClass = new MyClass();
      myClass.Name = "New Class Name";

      // Serialize the class, and close the TextWriter.
      TextWriter writer = new StreamWriter(filename);
      xSer.Serialize(writer, myClass);
      writer.Close();
   }

   // Return an XmlSerializer to override the root serialization.
   public XmlSerializer CreateOverrider()
   {
      // Create an XmlAttributes to override the default root element.
      XmlAttributes attrs = new XmlAttributes();

      // Create an XmlRootAttribute and set its element name and namespace.
      XmlRootAttribute xRoot = new XmlRootAttribute();
      xRoot.ElementName = "OverriddenRootElementName";
      xRoot.Namespace = "https://www.microsoft.com";

      // Set the XmlRoot property to the XmlRoot object.
      attrs.XmlRoot = xRoot;
      XmlAttributeOverrides xOver = new XmlAttributeOverrides();
      
      /* Add the XmlAttributes object to the 
      XmlAttributeOverrides object. */
      xOver.Add(typeof(MyClass), attrs);

      // Create the Serializer, and return it.
      XmlSerializer xSer = new XmlSerializer
      (typeof(MyClass), xOver);
      return xSer;
   }
}
#using <System.Xml.dll>
#using <System.dll>

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

// This is the class that is the default root element.
public ref class MyClass
{
public:
   String^ Name;
};

XmlSerializer^ CreateOverrider();
void SerializeOrder( String^ filename )
{
   // Create an XmlSerializer instance using the method below.
   XmlSerializer^ xSer = CreateOverrider();

   // Create the object, and set its Name property.
   MyClass^ myClass = gcnew MyClass;
   myClass->Name = "New Class Name";

   // Serialize the class, and close the TextWriter.
   TextWriter^ writer = gcnew StreamWriter( filename );
   xSer->Serialize( writer, myClass );
   writer->Close();
}

// Return an XmlSerializer to override the root serialization.
XmlSerializer^ CreateOverrider()
{
   // Create an XmlAttributes to override the default root element.
   XmlAttributes^ attrs = gcnew XmlAttributes;

   // Create an XmlRootAttribute and set its element name and namespace.
   XmlRootAttribute^ xRoot = gcnew XmlRootAttribute;
   xRoot->ElementName = "OverriddenRootElementName";
   xRoot->Namespace = "https://www.microsoft.com";

   // Set the XmlRoot property to the XmlRoot object.
   attrs->XmlRoot = xRoot;
   XmlAttributeOverrides^ xOver = gcnew XmlAttributeOverrides;

   /* Add the XmlAttributes object to the 
      XmlAttributeOverrides object. */
   xOver->Add( MyClass::typeid, attrs );

   // Create the Serializer, and return it.
   XmlSerializer^ xSer = gcnew XmlSerializer( MyClass::typeid,xOver );
   return xSer;
}

int main()
{
   SerializeOrder( "OverrideAttribute.xml" );
}
import System.*;
import System.IO.*;
import System.Xml.Serialization.*;

// This is the class that is the default root element.
public class MyClass
{
    public String name;
} //MyClass

public class Run
{
    public static void main(String[] args)
    {
        Run test = new Run();
        test.SerializeOrder("OverrideAttribute.xml");
    } //main

    public void SerializeOrder(String fileName)
    {
        // Create an XmlSerializer instance using the method below.
        XmlSerializer xSer = CreateOverrider();

        // Create the object, and set its Name property.
        MyClass myClass = new MyClass();
        myClass.name = "New Class Name";

        // Serialize the class, and close the TextWriter.
        TextWriter writer = new StreamWriter(fileName);
        xSer.Serialize(writer, myClass);
        writer.Close();
    } //SerializeOrder

    // Return an XmlSerializer to override the root serialization.
    public XmlSerializer CreateOverrider()
    {
        // Create an XmlAttributes to override the default root element.
        XmlAttributes attrs = new XmlAttributes();

        // Create an XmlRootAttribute and set its element name and namespace.
        XmlRootAttribute xRoot = new XmlRootAttribute();
        xRoot.set_ElementName("OverriddenRootElementName");
        xRoot.set_Namespace("https://www.microsoft.com");

        // Set the XmlRoot property to the XmlRoot object.
        attrs.set_XmlRoot(xRoot);
        XmlAttributeOverrides xOver = new XmlAttributeOverrides();

        /* Add the XmlAttributes object to the 
           XmlAttributeOverrides object. */
        xOver.Add(MyClass.class.ToType(), attrs);

        // Create the Serializer, and return it.
        XmlSerializer xSer = new XmlSerializer(MyClass.class.ToType(), xOver);
        return xSer;
    } //CreateOverrider
} //Run

プラットフォーム

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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。

バージョン情報

.NET Framework

サポート対象 : 2.0、1.1、1.0

.NET Compact Framework

サポート対象 : 2.0、1.0

参照

関連項目

XmlRootAttribute クラス
XmlRootAttribute メンバ
System.Xml.Serialization 名前空間
XmlAttributes.XmlRoot プロパティ
XmlAttributeOverrides クラス