XmlRootAttribute Klasa

Definicja

Formanty XML serializacji w celu atrybutu jako element główny 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
Dziedziczenie
XmlRootAttribute
Atrybuty

Przykłady

Poniższy przykład dotyczy XmlRootAttribute klasy . Atrybut określa nazwę elementu, przestrzeń nazw i to, czy element jest kwalifikowany, oraz czy xsi:nil atrybut jest generowany, jeśli klasa jest ustawiona na 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

Uwagi

Element XmlRootAttribute należy do rodziny atrybutów, które kontrolują sposób XmlSerializer serializacji lub deserializacji obiektu. Aby uzyskać pełną listę podobnych atrybutów, zobacz Atrybuty, które kontrolują serializacji XML.

Element można zastosować XmlRootAttribute do klasy, struktury, wyliczenia lub interfejsu. Można również zastosować atrybut do wartości zwracanej metody usługi sieci Web XML.

Każdy dokument XML musi mieć jeden element główny zawierający wszystkie inne elementy. Element XmlRootAttribute umożliwia kontrolowanie sposobu XmlSerializer generowania elementu głównego przez ustawienie określonych właściwości. Na przykład określ nazwę wygenerowanego elementu XML, ustawiając ElementName właściwość .

Aby uzyskać więcej informacji na temat używania atrybutów, zobacz Atrybuty.

Uwaga

Możesz użyć słowa XmlRoot w kodzie zamiast dłuższego XmlRootAttribute.

Konstruktory

XmlRootAttribute()

Inicjuje nowe wystąpienie klasy XmlRootAttribute.

XmlRootAttribute(String)

Inicjuje XmlRootAttribute nowe wystąpienie klasy i określa nazwę elementu głównego XML.

Właściwości

DataType

Pobiera lub ustawia typ danych XSD elementu głównego XML.

ElementName

Pobiera lub ustawia nazwę elementu XML, który jest generowany i rozpoznawany odpowiednio przez XmlSerializer metody i Deserialize(Stream) klasySerialize(TextWriter, Object).

IsNullable

Pobiera lub ustawia wartość wskazującą, czy XmlSerializer element członkowski musi serializować element członkowski ustawiony na null wartość w atrybucie ustawionym xsi:nil na truewartość .

Namespace

Pobiera lub ustawia przestrzeń nazw dla elementu głównego XML.

TypeId

Po zaimplementowaniu w klasie pochodnej pobiera unikatowy identyfikator dla tego Attributeelementu .

(Odziedziczone po Attribute)

Metody

Equals(Object)

Zwraca wartość wskazującą, czy to wystąpienie jest równe podanemu obiektowi.

(Odziedziczone po Attribute)
GetHashCode()

Zwraca wartość skrótu dla tego wystąpienia.

(Odziedziczone po Attribute)
GetType()

Type Pobiera wartość bieżącego wystąpienia.

(Odziedziczone po Object)
IsDefaultAttribute()

W przypadku zastąpienia w klasie pochodnej wskazuje, czy wartość tego wystąpienia jest wartością domyślną klasy pochodnej.

(Odziedziczone po Attribute)
Match(Object)

Po przesłonięciu w klasie pochodnej zwraca wartość wskazującą, czy to wystąpienie jest równe określonemu obiektowi.

(Odziedziczone po Attribute)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)

Jawne implementacje interfejsu

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

Zestaw nazw jest mapowany na odpowiedni zestaw identyfikatorów wysyłania.

(Odziedziczone po Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Pobiera informacje o typie dla obiektu, który może służyć do pobierania informacji o typie dla interfejsu.

(Odziedziczone po Attribute)
_Attribute.GetTypeInfoCount(UInt32)

Pobiera informację o liczbie typów interfejsów, jakie zawiera obiekt (0 lub 1).

(Odziedziczone po Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Umożliwia dostęp do właściwości i metod udostępnianych przez obiekt.

(Odziedziczone po Attribute)

Dotyczy

Zobacz też