Freigeben über


XmlAnyElementAttribute.Name-Eigenschaft

Ruft den XML-Elementnamen ab oder legt diesen fest.

Namespace: System.Xml.Serialization
Assembly: System.Xml (in system.xml.dll)

Syntax

'Declaration
Public Property Name As String
'Usage
Dim instance As XmlAnyElementAttribute
Dim value As String

value = instance.Name

instance.Name = value
public string Name { get; set; }
public:
property String^ Name {
    String^ get ();
    void set (String^ value);
}
/** @property */
public String get_Name ()

/** @property */
public void set_Name (String value)
public function get Name () : String

public function set Name (value : String)

Eigenschaftenwert

Der Name des XML-Elements.

Ausnahmen

Ausnahmetyp Bedingung

InvalidOperationException

Der Elementname eines Arraymembers stimmt nicht mit dem Elementnamen überein, der durch die Name-Eigenschaft angegeben wird.

Hinweise

Wenn Sie beim Anwenden des Attributs einen Name-Eigenschaftenwert angeben, müssen alle in das Array eingefügten XmlElement-Objekte oder XmlNode-Objekte über denselben Elementnamen und Standardnamespace verfügen, oder es wird eine Ausnahme ausgelöst. Wenn Sie den Namespace-Eigenschaftenwert festlegen, müssen Sie auch die Name-Eigenschaft festlegen, und die XmlElement-Objekte oder XmlNode-Objekte müssen denselben Namen und dieselben Namespacewerte aufweisen. Wenn kein Name-Wert angegeben wird, kann das XmlElement-Objekt oder das XmlNode-Objekt jeden Elementnamen annehmen.

Wenn Sie die Deserialize-Methode der XmlSerializer-Klasse aufrufen, werden alle Attribute im Array zusammengestellt, die über keinen entsprechenden Member in dem zu deserialisierenden Objekt verfügen. Wenn Sie einen Name-Wert angeben, enthält das Array nur die XML-Elemente mit diesem Namen. Wenn Sie keinen Name-Wert angeben, enthält das Array alle Elemente, die über keinen entsprechenden Member in der Klasse verfügen. Wenn eine Klasse mehrere Felder enthält, auf die das Attribut angewendet wird, verwenden Sie die Name-Eigenschaft und die Namespace-Eigenschaft zum Unterscheiden zwischen den Inhalten der Arrays. Wenn eine solche Klasse (mit mehreren Feldern) außerdem ein Feld ohne festgelegte unterscheidende Eigenschaftenwerte enthält (also Name und Namespace), enthält dieses Array bei der Deserialisierung alle XML-Elemente, die nicht bereits in den anderen Arrays vorhanden sind. Wenn Sie mehrere Felder hinzufügen, für die kein unterscheidender Name-Wert oder Namespace-Wert festgelegt ist, enthält das letzte Feld der Klasse alle unbekannten Elemente, die nicht schon in den anderen Arrays vorhanden sind, und alle anderen Felder werden auf NULL (Nothing in Visual Basic) festgelegt.

Sie können mehrere Instanzen von XmlAnyElementAttribute auf einen Klassenmember anwenden, jedoch muss jede Instanz über einen unterschiedlichen Name-Eigenschaftenwert verfügen. Wenn dieselbe Name-Eigenschaft für alle Instanzen festgelegt wurde, muss für jede Instanz ein unterschiedlicher Namespace-Eigenschaftenwert festgelegt werden.

Beispiel

Imports System
Imports System.Text
Imports System.IO
Imports System.Xml.Serialization
Imports System.Xml
Imports System.Xml.Schema


<XmlRoot(Namespace:= "http://www.cohowinery.com")> _
Public Class Group
   Public GroupName As String 

   ' This is for serializing Employee elements.
   <XmlAnyElement(Name:="Employee")> _
   Public  UnknownEmployees() As XmlElement

   ' This is for serializing City elements.   
   <XmlAnyElement _
   (Name:="City", _
   Namespace:="http://www.cpandl.com")> _
   Public UnknownCity() As XmlElement 

    ' This one is for all other unknown elements.
   <XmlAnyElement> _
   Public UnknownElements() As XmlElement 
End Class

Public Class Test
   Shared Sub Main()
      Dim t  As Test = New Test()
      t.SerializeObject("AnyElementArray.xml")
      t.DeserializeObject("AnyElementArray.xml")
      Console.WriteLine("Done")
   End Sub

   Private Sub SerializeObject(filename As String)
      Dim ser As XmlSerializer  = _
      New XmlSerializer(GetType(Group))
      ' Create an XmlNamespaces to use.
      Dim namespaces As XmlSerializerNamespaces = _
      New XmlSerializerNamespaces()
      namespaces.Add("c", "http://www.cohowinery.com")
      namespaces.Add("i", "http://www.cpandl.com")
      Dim myGroup As Group = New Group()
      ' Create arrays of arbitrary XmlElement objects.
      ' First create an XmlDocument, used to create the 
      ' XmlElement objects.
      Dim xDoc As XmlDocument = New XmlDocument()

      ' Create an array of Employee XmlElement objects.
      Dim El1 As XmlElement = xDoc.CreateElement("Employee", "http://www.cohowinery.com")
      El1.InnerText = "John"
      Dim El2 As XmlElement = xDoc.CreateElement("Employee", "http://www.cohowinery.com")
      El2.InnerText = "Joan"
      Dim El3 As XmlElement = xDoc.CreateElement("Employee", "http://www.cohowinery.com")
      El3.InnerText = "Jim"
      myGroup.UnknownEmployees= New XmlElement(){El1, El2, El3}     
    
      ' Create an array of City XmlElement objects.
      Dim inf1 As XmlElement = xDoc.CreateElement("City", "http://www.cpandl.com")
      inf1.InnerText = "Tokyo"
      Dim inf2 As XmlElement = xDoc.CreateElement("City", "http://www.cpandl.com")     
      inf2.InnerText = "New York"
      Dim inf3 As XmlElement = xDoc.CreateElement("City", "http://www.cpandl.com")     
      inf3.InnerText = "Rome"

      myGroup.UnknownCity = New XmlElement(){inf1, inf2, inf3}

      Dim xEl1 As XmlElement = xDoc.CreateElement("bld")
      xEl1.InnerText = "42"
      Dim xEl2 As XmlElement = xDoc.CreateElement("Region")
      xEl2.InnerText = "West"
      Dim xEl3 As XmlElement = xDoc.CreateElement("type")
      xEl3.InnerText = "Technical"
      myGroup.UnknownElements = _
      New XmlElement(){xEl1,xEl2,xEl3}
      ' Serialize the class, and close the TextWriter.
      Dim writer As TextWriter = New StreamWriter(filename)
      ser.Serialize(writer, myGroup, namespaces)
      writer.Close()

   End Sub

   Private Sub DeserializeObject(filename As String)
      Dim ser As XmlSerializer = _
      New XmlSerializer(GetType(Group))
      Dim fs As FileStream = New FileStream(filename, FileMode.Open)
      Dim myGroup As Group 
      myGroup = CType(ser.Deserialize(fs), Group)
      fs.Close()
      Dim xEmp As XmlElement
      for each xEmp in myGroup.UnknownEmployees
         Console.WriteLine(xEmp.LocalName & ": " & xEmp.InnerText)
      Next 
      
      Dim xCity As XmlElement
      for each xCity in myGroup.UnknownCity
         Console.WriteLine(xCity.LocalName & ": " & xCity.InnerText)
      Next
      
      Dim xEl As XmlElement
      for each  xEl in myGroup.UnknownElements
         Console.WriteLine(xEl.LocalName & ": " & xEl.InnerText)
      Next
   End Sub
 End Class
using System;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using System.Xml.Schema;


[XmlRoot(Namespace = "http://www.cohowinery.com")]
public class Group{
   public string GroupName;

   // This is for serializing Employee elements.
   [XmlAnyElement(Name = "Employee")]
   public XmlElement[] UnknownEmployees;

   // This is for serializing City elements.   
   [XmlAnyElement
   (Name = "City", 
   Namespace = "http://www.cpandl.com")]
   public XmlElement[] UnknownCity;

    // This one is for all other unknown elements.
   [XmlAnyElement]
   public XmlElement[] UnknownElements;
}

public class Test{
   static void Main(){
      Test t = new Test();
      t.SerializeObject("AnyElementArray.xml");
      t.DeserializeObject("AnyElementArray.xml");
      Console.WriteLine("Done");
   }

   private void SerializeObject(string filename){
      XmlSerializer ser = new XmlSerializer(typeof(Group));
      // Create an XmlNamespaces to use.
      XmlSerializerNamespaces namespaces =
      new XmlSerializerNamespaces();
      namespaces.Add("c", "http://www.cohowinery.com");
      namespaces.Add("i", "http://www.cpandl.com");
      Group myGroup = new Group();
      // Create arrays of arbitrary XmlElement objects.
      // First create an XmlDocument, used to create the 
      // XmlElement objects.
      XmlDocument xDoc = new XmlDocument();

      // Create an array of Employee XmlElement objects.
      XmlElement El1 = xDoc.CreateElement("Employee", "http://www.cohowinery.com");
      El1.InnerText = "John";
      XmlElement El2 = xDoc.CreateElement("Employee", "http://www.cohowinery.com");
      El2.InnerText = "Joan";
      XmlElement El3 = xDoc.CreateElement("Employee", "http://www.cohowinery.com");
      El3.InnerText = "Jim";
      myGroup.UnknownEmployees= new XmlElement[]{El1, El2, El3};     
    
      // Create an array of City XmlElement objects.
      XmlElement inf1 = xDoc.CreateElement("City", "http://www.cpandl.com");
      inf1.InnerText = "Tokyo";
      XmlElement inf2 = xDoc.CreateElement("City", "http://www.cpandl.com");     
      inf2.InnerText = "New York";
      XmlElement inf3 = xDoc.CreateElement("City", "http://www.cpandl.com");     
      inf3.InnerText = "Rome";

      myGroup.UnknownCity = new XmlElement[]{inf1, inf2, inf3};

      XmlElement xEl1 = xDoc.CreateElement("bld");
      xEl1.InnerText = "42";
      XmlElement xEl2 = xDoc.CreateElement("Region");
      xEl2.InnerText = "West";
      XmlElement xEl3 = xDoc.CreateElement("type");
      xEl3.InnerText = "Technical";
      myGroup.UnknownElements = 
        new XmlElement[]{xEl1,xEl2,xEl3};
      // Serialize the class, and close the TextWriter.
      TextWriter writer = new StreamWriter(filename);
      ser.Serialize(writer, myGroup, namespaces);
      writer.Close();

   }

   private void DeserializeObject(string filename){
      XmlSerializer ser = new XmlSerializer(typeof(Group));
      FileStream fs = new FileStream(filename, FileMode.Open);
      Group myGroup;
      myGroup = (Group)ser.Deserialize(fs);
      fs.Close();
      foreach(XmlElement xEmp in myGroup.UnknownEmployees){
         Console.WriteLine(xEmp.LocalName + ": " + xEmp.InnerText);}
      foreach(XmlElement xCity in myGroup.UnknownCity){
         Console.WriteLine(xCity.LocalName + ": " + xCity.InnerText);}
      foreach(XmlElement xEl in myGroup.UnknownElements){
         Console.WriteLine(xEl.LocalName + ": " + xEl.InnerText);}
   }
 }
#using <system.dll>
#using <system.xml.dll>

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

[XmlRoot(Namespace="http://www.cohowinery.com")]
public ref class Group
{
public:
   String^ GroupName;

   // This is for serializing Employee elements.

   [XmlAnyElement(Name="Employee")]
   array<XmlElement^>^UnknownEmployees;

   // This is for serializing City elements.   

   [XmlAnyElement
   (Name="City",
   Namespace="http://www.cpandl.com")]
   array<XmlElement^>^UnknownCity;

   // This one is for all other unknown elements.

   [XmlAnyElement]
   array<XmlElement^>^UnknownElements;
};

void SerializeObject( String^ filename )
{
   XmlSerializer^ ser = gcnew XmlSerializer( Group::typeid );

   // Create an XmlNamespaces to use.
   XmlSerializerNamespaces^ namespaces = gcnew XmlSerializerNamespaces;
   namespaces->Add( "c", "http://www.cohowinery.com" );
   namespaces->Add( "i", "http://www.cpandl.com" );
   Group^ myGroup = gcnew Group;

   // Create arrays of arbitrary XmlElement objects.
   // First create an XmlDocument, used to create the 
   // XmlElement objects.
   XmlDocument^ xDoc = gcnew XmlDocument;

   // Create an array of Employee XmlElement objects.
   XmlElement^ El1 = xDoc->CreateElement( "Employee", "http://www.cohowinery.com" );
   El1->InnerText = "John";
   XmlElement^ El2 = xDoc->CreateElement( "Employee", "http://www.cohowinery.com" );
   El2->InnerText = "Joan";
   XmlElement^ El3 = xDoc->CreateElement( "Employee", "http://www.cohowinery.com" );
   El3->InnerText = "Jim";
   array<XmlElement^>^employees = {El1,El2,El3};
   myGroup->UnknownEmployees = employees;

   // Create an array of City XmlElement objects.
   XmlElement^ inf1 = xDoc->CreateElement( "City", "http://www.cpandl.com" );
   inf1->InnerText = "Tokyo";
   XmlElement^ inf2 = xDoc->CreateElement( "City", "http://www.cpandl.com" );
   inf2->InnerText = "New York";
   XmlElement^ inf3 = xDoc->CreateElement( "City", "http://www.cpandl.com" );
   inf3->InnerText = "Rome";
   array<XmlElement^>^cities = {inf1,inf2,inf3};
   myGroup->UnknownCity = cities;
   XmlElement^ xEl1 = xDoc->CreateElement( "bld" );
   xEl1->InnerText = "42";
   XmlElement^ xEl2 = xDoc->CreateElement( "Region" );
   xEl2->InnerText = "West";
   XmlElement^ xEl3 = xDoc->CreateElement( "type" );
   xEl3->InnerText = "Technical";
   array<XmlElement^>^elements = {xEl1,xEl2,xEl3};
   myGroup->UnknownElements = elements;

   // Serialize the class, and close the TextWriter.
   TextWriter^ writer = gcnew StreamWriter( filename );
   ser->Serialize( writer, myGroup, namespaces );
   writer->Close();
}

void DeserializeObject( String^ filename )
{
   XmlSerializer^ ser = gcnew XmlSerializer( Group::typeid );
   FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
   Group^ myGroup;
   myGroup = safe_cast<Group^>(ser->Deserialize( fs ));
   fs->Close();
   for ( int i = 0; i < myGroup->UnknownEmployees->Length; ++i )
   {
      XmlElement^ xEmp = myGroup->UnknownEmployees[ i ];
      Console::WriteLine( "{0}: {1}", xEmp->LocalName, xEmp->InnerText );
   }
   for ( int i = 0; i < myGroup->UnknownCity->Length; ++i )
   {
      XmlElement^ xCity = myGroup->UnknownCity[ i ];
      Console::WriteLine( "{0}: {1}", xCity->LocalName, xCity->InnerText );
   }
   for ( int i = 0; i < myGroup->UnknownElements->Length; ++i )
   {
      XmlElement^ xEmp = myGroup->UnknownElements[ i ];
      Console::WriteLine( "{0}: {1}", xEmp->LocalName, xEmp->InnerText );
   }
}

int main()
{
   SerializeObject(  "AnyElementArray.xml" );
   DeserializeObject(  "AnyElementArray.xml" );
   Console::WriteLine(  "Done" );
}
import System.*;
import System.Text.*;
import System.IO.*;
import System.Xml.Serialization.*;
import System.Xml.*;
import System.Xml.Schema.*;
/** @attribute XmlRoot(Namespace = "http://www.cohowinery.com")
 */
public class Group
{
    public String groupName;

    // This is for serializing Employee elements.
    /** @attribute XmlAnyElement(Name = "Employee")
     */
    public XmlElement unknownEmployees[];

    // This is for serializing City elements.   
    /** @attribute XmlAnyElement(Name = "City", Namespace =
        "http://www.cpandl.com")
     */
    public XmlElement unknownCity[];

    // This one is for all other unknown elements.
    /** @attribute XmlAnyElement()
     */
    public XmlElement unknownElements[];
} //Group

public class Test
{
    public static void main(String[] args)
    {
        Test t = new Test();
        t.SerializeObject("AnyElementArray.xml");
        t.DeserializeObject("AnyElementArray.xml");
        Console.WriteLine("Done");
    } //main

    private void SerializeObject(String fileName)
    {
        XmlSerializer ser = new XmlSerializer(Group.class.ToType());
        // Create an XmlNamespaces to use.
        XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
        namespaces.Add("c", "http://www.cohowinery.com");
        namespaces.Add("i", "http://www.cpandl.com");
        Group myGroup = new Group();
        // Create arrays of arbitrary XmlElement objects.
        // First create an XmlDocument, used to create the 
        // XmlElement objects.
        XmlDocument xDoc = new XmlDocument();
        // Create an array of Employee XmlElement objects.
        XmlElement el1 = xDoc.CreateElement("Employee",
                "http://www.cohowinery.com");
        el1.set_InnerText("John");
        XmlElement el2 = xDoc.CreateElement("Employee", 
                "http://www.cohowinery.com");
        el2.set_InnerText("Joan");
        XmlElement el3 = xDoc.CreateElement("Employee", 
                "http://www.cohowinery.com");
        el3.set_InnerText("Jim");
        myGroup.unknownEmployees = new XmlElement[] { el1, el2, el3 };
        // Create an array of City XmlElement objects.
        XmlElement inf1 = xDoc.CreateElement("City", "http://www.cpandl.com");
        inf1.set_InnerText("Tokyo");
        XmlElement inf2 = xDoc.CreateElement("City", "http://www.cpandl.com");
        inf2.set_InnerText("New York");
        XmlElement inf3 = xDoc.CreateElement("City", "http://www.cpandl.com");
        inf3.set_InnerText("Rome");

        myGroup.unknownCity = new XmlElement[] { inf1, inf2, inf3 };

        XmlElement xEl1 = xDoc.CreateElement("bld");
        xEl1.set_InnerText("42");
        XmlElement xEl2 = xDoc.CreateElement("Region");
        xEl2.set_InnerText("West");
        XmlElement xEl3 = xDoc.CreateElement("type");
        xEl3.set_InnerText("Technical");
        myGroup.unknownElements = new XmlElement[] { xEl1, xEl2, xEl3 };
        // Serialize the class, and close the TextWriter.
        TextWriter writer = new StreamWriter(fileName);
        ser.Serialize(writer, myGroup, namespaces);
        writer.Close();
    } //SerializeObject

    private void DeserializeObject(String fileName)
    {
        XmlSerializer ser = new XmlSerializer(Group.class.ToType());
        FileStream fs = new FileStream(fileName, FileMode.Open);
        Group myGroup;
        myGroup = (Group)ser.Deserialize(fs);
        fs.Close();
        for (int iCtr = 0;iCtr < myGroup.unknownEmployees.get_Count();iCtr++) {
            XmlElement xEmp = (XmlElement)myGroup.unknownEmployees.
                get_Item(iCtr);
            Console.WriteLine(xEmp.get_LocalName() + ": " 
                + xEmp.get_InnerText());
        }
        for (int iCtr = 0; iCtr < myGroup.unknownCity.get_Count(); iCtr++) {
            XmlElement xCity = (XmlElement)myGroup.unknownCity.get_Item(iCtr);
            Console.WriteLine(xCity.get_LocalName() + ": " 
                + xCity.get_InnerText());
        }
        for (int iCtr = 0;iCtr < myGroup.unknownElements.get_Count();iCtr++) {
            XmlElement xEl = (XmlElement)myGroup.unknownElements.get_Item(iCtr);
            Console.WriteLine(xEl.get_LocalName() + ": " + xEl.get_InnerText());
        }
    } //DeserializeObject
} //Test 

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

XmlAnyElementAttribute-Klasse
XmlAnyElementAttribute-Member
System.Xml.Serialization-Namespace