XmlAnyAttributeAttribute Classe

Definizione

Consente di specificare l'inserimento di qualsiasi attributo XML nel membro, ovvero in un campo che restituisce una matrice di oggetti XmlAttribute.

public ref class XmlAnyAttributeAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue, AllowMultiple=false)]
public class XmlAnyAttributeAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue)]
public class XmlAnyAttributeAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue, AllowMultiple=false)>]
type XmlAnyAttributeAttribute = class
    inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue)>]
type XmlAnyAttributeAttribute = class
    inherit Attribute
Public Class XmlAnyAttributeAttribute
Inherits Attribute
Ereditarietà
XmlAnyAttributeAttribute
Attributi

Esempio

Nell'esempio seguente vengono raccolti tutti gli attributi sconosciuti in una matrice di XmlAttribute oggetti. Per provare l'esempio, creare un file denominato UnknownAttributes.xml contenente il codice XML seguente:

<?xml version="1.0" encoding="utf-8"?>  
<Group xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
xmlns:xsd="http://www.w3.org/2001/XMLSchema"   
GroupType = 'Technical' GroupNumber = '42' GroupBase = 'Red'>  
  <GroupName>MyGroup</GroupName>  
</Group>  
#using <System.dll>
#using <System.Xml.dll>

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

public ref class Group
{
public:
   String^ GroupName;

   // The UnknownAttributes array will be used to collect all unknown
   // attributes found when deserializing.

   [XmlAnyAttributeAttribute]
   array<XmlAttribute^>^XAttributes;
};

void SerializeObject( String^ filename, Object^ g )
{
   XmlSerializer^ ser = gcnew XmlSerializer( Group::typeid );
   TextWriter^ writer = gcnew StreamWriter( filename );
   ser->Serialize( writer, g );
   writer->Close();
}

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

   // A FileStream is needed to read the XML document.
   FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
   Group^ g = safe_cast<Group^>(ser->Deserialize( fs ));
   fs->Close();

   // Write out the data, including unknown attributes.
   Console::WriteLine( g->GroupName );
   Console::WriteLine(  "Number of unknown attributes: {0}", g->XAttributes->Length );
   for ( IEnumerator ^ e = g->XAttributes->GetEnumerator(); e->MoveNext();  )
   {
      XmlAttribute^ xAtt = safe_cast<XmlAttribute^>(e->Current);
      Console::WriteLine( "{0}: {1}", xAtt->Name, xAtt->InnerXml );
   }
   SerializeObject( "AttributesAdded.xml", g );
}

int main()
{
   // Deserialize the file containing unknown attributes.
   DeserializeObject(  "UnknownAttributes.xml" );
}
using System;
using System.IO;
using System.Xml.Serialization;
using System.Xml;

public class Group{
   public string GroupName;
   // The UnknownAttributes array will be used to collect all unknown
   // attributes found when deserializing.
   [XmlAnyAttribute]
    public XmlAttribute[]XAttributes;
}

public class Test{
   static void Main(){
      Test t = new Test();
      // Deserialize the file containing unknown attributes.

      t.DeserializeObject("UnknownAttributes.xml");
   }

   private void DeserializeObject(string filename){
      XmlSerializer ser = new XmlSerializer(typeof(Group));
      // A FileStream is needed to read the XML document.
     FileStream fs = new FileStream(filename, FileMode.Open);
     Group g = (Group) ser.Deserialize(fs);
     fs.Close();
     // Write out the data, including unknown attributes.
     Console.WriteLine(g.GroupName);
     Console.WriteLine("Number of unknown attributes: " + 
     g.XAttributes.Length);
     foreach(XmlAttribute xAtt in g.XAttributes){
     Console.WriteLine(xAtt.Name + ": " + xAtt.InnerXml);
     }
     // Serialize the object again with the attributes added.
     this.SerializeObject("AttributesAdded.xml",g);
   }

   private void SerializeObject(string filename, object g){
      XmlSerializer ser = new XmlSerializer(typeof(Group));
      TextWriter writer = new StreamWriter(filename);
      ser.Serialize(writer, g);
      writer.Close();
   }
}
Imports System.IO
Imports System.Xml.Serialization
Imports System.Xml

Public Class Group
   Public GroupName As String 
   ' The UnknownAttributes array will be used to collect all unknown
   ' attributes found when deserializing.
   <XmlAnyAttribute> _
    Public UnknownAttributes()As XmlAttribute
End Class

Public Class Test
   Shared Sub Main()
      Dim t  As Test = New Test()
      ' Deserialize the file containing unknown attributes.
      t.DeserializeObject("UnknownAttributes.xml")
   End Sub

   Private Sub DeserializeObject(filename As String)
      Dim ser As XmlSerializer = New XmlSerializer(GetType(Group))
      ' A FileStream is needed to read the XML document.
     Dim fs As FileStream = New FileStream(filename, FileMode.Open)
     Dim g As Group = CType(ser.Deserialize(fs), Group)
     fs.Close()
     ' Write out the data, including unknown attributes.
     Console.WriteLine(g.GroupName)
     Console.WriteLine("Number of unknown attributes: " & _ 
     g.UnknownAttributes.Length)
     Dim xAtt As XmlAttribute
     for each xAtt in g.UnknownAttributes
        Console.WriteLine(xAtt.Name & ": " & xAtt.InnerXml)
     Next
     ' Serialize the object again with the attributes added.
     Me.SerializeObject("AttributesAdded.xml",g)
   End Sub

   Private Sub SerializeObject(filename As String, g As object)
      Dim ser As XmlSerializer = New XmlSerializer(GetType(Group))
      DIm writer As TextWriter = New StreamWriter(filename)
      ser.Serialize(writer, g)
      writer.Close()
   End Sub
End Class

Commenti

Usare l'oggetto XmlAnyAttributeAttribute per contenere dati arbitrari (come attributi XML) inviati come parte di un documento XML, ad esempio i metadati inviati come parte del documento.

Applicare l'oggetto XmlAnyAttributeAttribute a un campo che restituisce una matrice di XmlAttribute oggetti o XmlNode . Quando viene chiamato il Deserialize metodo della classe, tutti gli attributi XML che non dispongono di XmlSerializer un membro corrispondente nella classe deserializzata vengono raccolti nella matrice. Dopo la deserializzazione, è possibile scorrere la raccolta di XmlAttribute elementi per elaborare i dati.

Gli UnknownNode eventi e UnknownAttribute dell'oggetto XmlSerializer non si verificano se si applica l'oggetto XmlAnyAttributeAttribute a un membro di una classe.

Nota

È possibile usare la parola XmlAnyAttribute nel codice anziché più lunga XmlAnyAttributeAttribute.

Per altre informazioni sull'uso degli attributi, vedere Attributi.

Costruttori

XmlAnyAttributeAttribute()

Consente di creare una nuova istanza della classe XmlAnyAttributeAttribute.

Proprietà

TypeId

Quando è implementata in una classe derivata, ottiene un identificatore univoco della classe Attribute.

(Ereditato da Attribute)

Metodi

Equals(Object)

Restituisce un valore che indica se questa istanza è uguale a un oggetto specificato.

(Ereditato da Attribute)
GetHashCode()

Restituisce il codice hash per l'istanza.

(Ereditato da Attribute)
GetType()

Ottiene l'oggetto Type dell'istanza corrente.

(Ereditato da Object)
IsDefaultAttribute()

In caso di override in una classe derivata, indica se il valore di questa istanza è il valore predefinito per la classe derivata.

(Ereditato da Attribute)
Match(Object)

Quando è sottoposto a override in una classe derivata, restituisce un valore che indica se questa istanza equivale a un oggetto specificato.

(Ereditato da Attribute)
MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.

(Ereditato da Object)
ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Implementazioni dell'interfaccia esplicita

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

Esegue il mapping di un set di nomi a un set corrispondente di ID dispatch.

(Ereditato da Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Recupera le informazioni sul tipo relative a un oggetto, che possono essere usate per ottenere informazioni sul tipo relative a un'interfaccia.

(Ereditato da Attribute)
_Attribute.GetTypeInfoCount(UInt32)

Recupera il numero delle interfacce di informazioni sul tipo fornite da un oggetto (0 o 1).

(Ereditato da Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Fornisce l'accesso a proprietà e metodi esposti da un oggetto.

(Ereditato da Attribute)

Si applica a

Vedi anche