XmlRootAttribute 類別

定義

控制做為 XML 根項目之屬性目標的 XML 序列化。

public ref class XmlRootAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Enum | System.AttributeTargets.Interface | System.AttributeTargets.ReturnValue | System.AttributeTargets.Struct)]
public class XmlRootAttribute : 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 套用至 類別。 屬性會指定專案名稱、命名空間,以及專案是否限定,以及如果類別設定 null 為 ,是否 xsi:nil 產生屬性。

#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 。 如需類似屬性的完整清單,請參閱 控制 XML 序列化的屬性

您可以將 套用 XmlRootAttribute 至類別、結構、列舉或介面。 您也可以將 屬性套用至 XML Web 服務方法的傳回值。

每個 XML 檔都必須有包含所有其他元素的單一根項目。 XmlRootAttribute可讓您藉由設定特定屬性來控制 產生根項目的方式 XmlSerializer 。 例如,藉由設定 ElementName 屬性來指定產生的 XML 專案名稱。

如需使用屬性的詳細資訊,請參閱 屬性

注意

您可以在程式碼中使用這個字, XmlRoot 而不是較長 XmlRootAttribute 的 。

建構函式

XmlRootAttribute()

初始化 XmlRootAttribute 類別的新執行個體。

XmlRootAttribute(String)

初始化 XmlRootAttribute 類別的新執行個體,並指定 XML 根項目的名稱。

屬性

DataType

取得或設定 XML 根項目的 XSD 資料類型。

ElementName

取得或設定分別由 XmlSerializer 類別的 Serialize(TextWriter, Object)Deserialize(Stream) 方法所產生和辨識的 XML 項目。

IsNullable

取得或設定值,指出 XmlSerializer 是否必須將設為 null 的成員序列化成設為 truexsi:nil 屬性。

Namespace

取得或設定 XML 根項目的命名空間。

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。

(繼承來源 Attribute)

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。

(繼承來源 Attribute)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

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

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

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

擷取物件的類型資訊,可以用來取得介面的類型資訊。

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

擷取物件提供的類型資訊介面數目 (0 或 1)。

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

提供物件所公開的屬性和方法的存取權。

(繼承來源 Attribute)

適用於

另請參閱