次の方法で共有


XmlRootAttribute コンストラクタ (String)

XmlRootAttribute クラスの新しいインスタンスを初期化し、XML ルート要素の名前を指定します。

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

構文

'宣言
Public Sub New ( _
    elementName As String _
)
'使用
Dim elementName As String

Dim instance As New XmlRootAttribute(elementName)
public XmlRootAttribute (
    string elementName
)
public:
XmlRootAttribute (
    String^ elementName
)
public XmlRootAttribute (
    String elementName
)
public function XmlRootAttribute (
    elementName : String
)

パラメータ

  • elementName
    XML ルート要素の名前。

使用例

次の例では、XmlRootAttribute のインスタンスを作成し、そのインスタンスを使用して、"Student" というクラスのインスタンスのシリアル化をオーバーライドします。

Public Sub SerializeOrder(filename As String)
   ' Create an XmlSerializer instance using the method below.
   Dim myXmlSerializer As XmlSerializer = CreateOverrider()

   ' Create the object, and set its Name property.
   Dim myStudent As New Student()
   myStudent.Name = "Student class1"

   ' Serialize the class, and close the TextWriter.
   Dim writer = New StreamWriter(filename)
   myXmlSerializer.Serialize(writer, myStudent)
   writer.Close()
End Sub

' Return an XmlSerializer to override the root serialization.
Public Function CreateOverrider() As XmlSerializer
   ' Create an XmlAttributes to override the default root element.
   Dim myXmlAttributes As New XmlAttributes()

   ' Create an XmlRootAttribute overloaded constructer and set its namespace.
   Dim myXmlRootAttribute As New XmlRootAttribute("OverriddenRootElementName")
   myXmlRootAttribute.Namespace = "https://www.microsoft.com"

   ' Set the XmlRoot property to the XmlRoot object.
   myXmlAttributes.XmlRoot = myXmlRootAttribute
   Dim myXmlAttributeOverrides As New XmlAttributeOverrides()

   ' Add the XmlAttributes object to the XmlAttributeOverrides object.
   myXmlAttributeOverrides.Add(GetType(Student), myXmlAttributes)

   ' Create the Serializer, and return it.
   Dim myXmlSerializer As New XmlSerializer(GetType(Student), myXmlAttributeOverrides)
   Return myXmlSerializer
End Function
public void SerializeOrder(string filename)
{
   // Create an XmlSerializer instance using the method below.
   XmlSerializer myXmlSerializer = CreateOverrider();

   // Create the object, and set its Name property.
   Student myStudent = new Student();
   myStudent.Name = "Student class1";

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

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

   // Create an XmlRootAttribute overloaded constructer 
   //and set its namespace.
   XmlRootAttribute myXmlRootAttribute = 
                  new XmlRootAttribute("OverriddenRootElementName");
   myXmlRootAttribute.Namespace = "https://www.microsoft.com";

   // Set the XmlRoot property to the XmlRoot object.
   myXmlAttributes.XmlRoot = myXmlRootAttribute;
   XmlAttributeOverrides myXmlAttributeOverrides = 
                                       new XmlAttributeOverrides();
   
   /* Add the XmlAttributes object to the 
   XmlAttributeOverrides object. */
   myXmlAttributeOverrides.Add(typeof(Student), myXmlAttributes);

   // Create the Serializer, and return it.
   XmlSerializer myXmlSerializer = new XmlSerializer
      (typeof(Student), myXmlAttributeOverrides);
   return myXmlSerializer;
}
public:
   void SerializeOrder( String^ filename )
   {
      // Create an XmlSerializer instance using the method below.
      XmlSerializer^ myXmlSerializer = CreateOverrider();
      
      // Create the object, and set its Name property.
      Student^ myStudent = gcnew Student;
      myStudent->Name = "Student class1";
      
      // Serialize the class, and close the TextWriter.
      TextWriter^ writer = gcnew StreamWriter( filename );
      myXmlSerializer->Serialize( writer, myStudent );
      writer->Close();
   }

   // Return an XmlSerializer to  the root serialization.
   XmlSerializer^ CreateOverrider()
   {
      // Create an XmlAttributes to  the default root element.
      XmlAttributes^ myXmlAttributes = gcnew XmlAttributes;
      
      // Create an XmlRootAttribute overloaded constructer 
      // and set its namespace.
      XmlRootAttribute^ myXmlRootAttribute =
         gcnew XmlRootAttribute( "OverriddenRootElementName" );
      myXmlRootAttribute->Namespace = "https://www.microsoft.com";
      
      // Set the XmlRoot property to the XmlRoot object.
      myXmlAttributes->XmlRoot = myXmlRootAttribute;
      XmlAttributeOverrides^ myXmlAttributeOverrides =
         gcnew XmlAttributeOverrides;
      
      // Add the XmlAttributes object to the XmlAttributeOverrides object
      myXmlAttributeOverrides->Add( Student::typeid, myXmlAttributes );
      
      // Create the Serializer, and return it.
      XmlSerializer^ myXmlSerializer = gcnew XmlSerializer(
         Student::typeid, myXmlAttributeOverrides );
      return myXmlSerializer;
   }
public void SerializeOrder(String fileName)
{
    // Create an XmlSerializer instance using the method below.
    XmlSerializer myXmlSerializer = CreateOverrider();
    // Create the object, and set its name property.
    Student myStudent = new Student();
    myStudent.name = "Student class1";
    // Serialize the class, and close the TextWriter.
    TextWriter writer = new StreamWriter(fileName);
    myXmlSerializer.Serialize(writer, myStudent);
    writer.Close();
} //SerializeOrder

// Return an XmlSerializer to override the root serialization.
public XmlSerializer CreateOverrider()
{
    // Create an XmlAttributes to override the default root element.
    XmlAttributes myXmlAttributes = new XmlAttributes();
    // Create an XmlRootAttribute overloaded constructer 
    //and set its namespace.
    XmlRootAttribute myXmlRootAttribute = new XmlRootAttribute(
        "OverriddenRootElementName");
    myXmlRootAttribute.set_Namespace("https://www.microsoft.com");
    // Set the XmlRoot property to the XmlRoot object.
    myXmlAttributes.set_XmlRoot(myXmlRootAttribute);
    XmlAttributeOverrides myXmlAttributeOverrides =
        new XmlAttributeOverrides();

    /* Add the XmlAttributes object to the 
       XmlAttributeOverrides object.
     */
    myXmlAttributeOverrides.Add(Student.class.ToType(), myXmlAttributes);
    // Create the Serializer, and return it.
    XmlSerializer myXmlSerializer = new XmlSerializer(
        Student.class.ToType(), myXmlAttributeOverrides);
    return myXmlSerializer;
} //CreateOverrider

プラットフォーム

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 名前空間
ElementName