XmlRootAttribute 類別

定義

控制做為 XML 根項目之屬性目標的 XML 序列化。Controls XML serialization of the attribute target as an XML root element.

public ref class XmlRootAttribute : Attribute
public class XmlRootAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Enum | System.AttributeTargets.Interface | System.AttributeTargets.ReturnValue | System.AttributeTargets.Struct)]
public class XmlRootAttribute : Attribute
type XmlRootAttribute = class
    inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Enum | System.AttributeTargets.Interface | System.AttributeTargets.ReturnValue | System.AttributeTargets.Struct)>]
type XmlRootAttribute = class
    inherit Attribute
Public Class XmlRootAttribute
Inherits Attribute
繼承
XmlRootAttribute
屬性

範例

下列範例會將套用 XmlRootAttribute 至類別。The following example applies the XmlRootAttribute to a class. 屬性會指定專案名稱、命名空間,以及專案是否為限定的,以及是否 xsi:nil 要在類別設定為時產生屬性 nullThe attribute specifies the element name, namespace, and whether the element is qualified, and whether the xsi:nil attribute is generated if the class is set to null.

#using <System.Xml.dll>
#using <System.dll>

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

[XmlRoot(Namespace="www.contoso.com",
ElementName="MyGroupName",
DataType="string",
IsNullable=true)]
public ref class Group
{
private:
   String^ groupNameValue;

public:

   // Insert code for the Group class.
   Group(){}

   Group( String^ groupNameVal )
   {
      groupNameValue = groupNameVal;
   }

   property String^ GroupName 
   {
      String^ get()
      {
         return groupNameValue;
      }
      void set( String^ value )
      {
         groupNameValue = value;
      }

   }

};

void SerializeGroup()
{
   // Create an instance of the Group class, and an
   // instance of the XmlSerializer to serialize it.
   Group^ myGroup = gcnew Group( "Redmond" );
   XmlSerializer^ ser = gcnew XmlSerializer( Group::typeid );

   // A FileStream is used to write the file.
   FileStream^ fs = gcnew FileStream( "group.xml",FileMode::Create );
   ser->Serialize( fs, myGroup );
   fs->Close();
   Console::WriteLine( myGroup->GroupName );
   Console::WriteLine( "Done" );
   Console::ReadLine();
}

int main()
{
   SerializeGroup();
}
using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

[XmlRoot(Namespace = "www.contoso.com",
     ElementName = "MyGroupName",
     DataType = "string",
     IsNullable=true)]
public class Group
{
    private string groupNameValue;
    // Insert code for the Group class.
    public Group()
    {
    }

    public Group(string groupNameVal)
    {
        groupNameValue = groupNameVal;
    }

    public string GroupName
    {
        get{return groupNameValue;}
        set{groupNameValue = value;}
    }
}
public class Test
{
    static void Main()
    {
        Test t = new Test();
        t.SerializeGroup();
    }

    private void SerializeGroup()
    {
        // Create an instance of the Group class, and an
        // instance of the XmlSerializer to serialize it.
        Group myGroup = new Group("Redmond");
        XmlSerializer ser = new XmlSerializer(typeof(Group));
        // A FileStream is used to write the file.
        FileStream fs = new FileStream("group.xml",FileMode.Create);
        ser.Serialize(fs,myGroup);
        fs.Close();
        Console.WriteLine(myGroup.GroupName);
        Console.WriteLine("Done");
        Console.ReadLine();
    }
}
Imports System.IO
Imports System.Xml
Imports System.Xml.Schema
Imports System.Xml.Serialization

<XmlRoot(Namespace:="www.contoso.com", _
    ElementName:="MyGroupName", _
    DataType:="string", _
    IsNullable:=True)> _
Public Class Group

    Private groupNameValue As String
    ' Insert code for the Group class.
    Public Sub New()

    End Sub

    Public Sub New(ByVal groupNameVal As String)

        groupNameValue = groupNameVal
    End Sub

    Property GroupName() As String
        Get
            Return groupNameValue
        End Get

        Set(ByVal Value As String)
            groupNameValue = Value
        End Set
    End Property
End Class

Public Class Test

    Shared Sub Main()

        Dim t As Test = New Test()
        t.SerializeGroup()
    End Sub

    Private Sub SerializeGroup()

        ' Create an instance of the Group class, and an
        ' instance of the XmlSerializer to serialize it.
        Dim myGroup As Group = New Group("Redmond")
        Dim ser As XmlSerializer = New XmlSerializer(GetType(Group))

        ' A FileStream is used to write the file.
        Dim fs As FileStream = New FileStream("group.xml", FileMode.Create)
        ser.Serialize(fs, myGroup)
        fs.Close()
        Console.WriteLine(myGroup.GroupName)
        Console.WriteLine("Done... Press any key to exit.")
        Console.ReadLine()
    End Sub

End Class

備註

XmlRootAttribute屬於一系列屬性,可控制序列化或還原序列化 XmlSerializer 物件的方式。The XmlRootAttribute belongs to a family of attributes that controls how the XmlSerializer serializes or deserializes an object. 如需類似屬性的完整清單,請參閱 控制 XML 序列化的屬性For a complete list of similar attributes, see Attributes That Control XML Serialization.

您可以將套用 XmlRootAttribute 至類別、結構、列舉或介面。You can apply the XmlRootAttribute to a class, structure, enumeration, or interface. 您也可以將屬性套用至 XML Web Service 方法的傳回值。You can also apply the attribute to the return value of an XML Web service method.

每個 XML 檔都必須有一個包含所有其他元素的單一根項目。Every XML document must have a single root element that contains all the other elements. XmlRootAttribute 讓您控制如何藉 XmlSerializer 由設定特定屬性來產生根項目。The XmlRootAttribute allows you to control how the XmlSerializer generates the root element by setting certain properties. 例如,藉由設定屬性,指定產生之 XML 專案的名稱 ElementNameFor example, specify the name of the generated XML element by setting the ElementName property.

如需使用屬性的詳細資訊,請參閱 屬性For more information about using attributes, see Attributes.

注意

您可以 XmlRoot 在程式碼中使用這個字,而不是使用較長的時間 XmlRootAttributeYou can use the word XmlRoot in your code instead of the longer XmlRootAttribute.

建構函式

XmlRootAttribute()

初始化 XmlRootAttribute 類別的新執行個體。Initializes a new instance of the XmlRootAttribute class.

XmlRootAttribute(String)

初始化 XmlRootAttribute 類別的新執行個體,並指定 XML 根項目的名稱。Initializes a new instance of the XmlRootAttribute class and specifies the name of the XML root element.

屬性

DataType

取得或設定 XML 根項目的 XSD 資料類型。Gets or sets the XSD data type of the XML root element.

ElementName

取得或設定分別由 XmlSerializer 類別的 Serialize(TextWriter, Object)Deserialize(Stream) 方法所產生和辨識的 XML 項目。Gets or sets the name of the XML element that is generated and recognized by the XmlSerializer class's Serialize(TextWriter, Object) and Deserialize(Stream) methods, respectively.

IsNullable

取得或設定值,指出 XmlSerializer 是否必須將設為 null 的成員序列化成設為 truexsi:nil 屬性。Gets or sets a value that indicates whether the XmlSerializer must serialize a member that is set to null into the xsi:nil attribute set to true.

Namespace

取得或設定 XML 根項目的命名空間。Gets or sets the namespace for the XML root element.

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。When implemented in a derived class, gets a unique identifier for this Attribute.

(繼承來源 Attribute)

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。Returns a value that indicates whether this instance is equal to a specified object.

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。Returns the hash code for this instance.

(繼承來源 Attribute)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

明確介面實作

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。Maps a set of names to a corresponding set of dispatch identifiers.

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。Retrieves the type information for an object, which can be used to get the type information for an interface.

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。Provides access to properties and methods exposed by an object.

(繼承來源 Attribute)

適用於

另請參閱