다음을 통해 공유


XmlRootAttribute 생성자

정의

XmlRootAttribute 클래스의 새 인스턴스를 초기화하고 해당 클래스 이름을 XML 루트 요소의 이름으로 사용합니다.

오버로드

XmlRootAttribute()

XmlRootAttribute 클래스의 새 인스턴스를 초기화합니다.

XmlRootAttribute(String)

XmlRootAttribute 클래스의 새 인스턴스를 초기화하고 XML 루트 요소의 이름을 지정합니다.

XmlRootAttribute()

XmlRootAttribute 클래스의 새 인스턴스를 초기화합니다.

public:
 XmlRootAttribute();
public XmlRootAttribute ();
Public Sub New ()

예제

다음 예제에서는 인스턴스를 XmlRootAttribute 만들고 개체의 XmlAttributes 속성에 XmlRoot 할당합니다. 개체를 XmlSerializer 직렬화 MyClass 할 때 개체를 XmlRootAttribute 사용하여 기본 루트 요소를 재정의합니다.

#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 = "http://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" );
}
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 = "http://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;
   }
}
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
    
    
    ' 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 = "http://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

추가 정보

적용 대상

XmlRootAttribute(String)

XmlRootAttribute 클래스의 새 인스턴스를 초기화하고 XML 루트 요소의 이름을 지정합니다.

public:
 XmlRootAttribute(System::String ^ elementName);
public XmlRootAttribute (string elementName);
new System.Xml.Serialization.XmlRootAttribute : string -> System.Xml.Serialization.XmlRootAttribute
Public Sub New (elementName As String)

매개 변수

elementName
String

XML 루트 요소의 이름입니다.

예제

다음 예제에서는 XmlRootAttribute의 인스턴스를 만들고 이를 사용하여 "Student"이라는 클래스 인스턴스의 serialization을 재정의합니다.

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 = "http://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();
}

// 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 = "http://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 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 = "http://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

추가 정보

적용 대상