Assembly: System.Xml (in system.xml.dll)
<AttributeUsageAttribute(AttributeTargets.Property Or AttributeTargets.Field Or AttributeTargets.Parameter Or AttributeTargets.ReturnValue, AllowMultiple:=False)> _ Public Class XmlAnyAttributeAttribute Inherits Attribute
Dim instance As XmlAnyAttributeAttribute
[AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Parameter|AttributeTargets.ReturnValue, AllowMultiple=false)] public class XmlAnyAttributeAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::Property|AttributeTargets::Field|AttributeTargets::Parameter|AttributeTargets::ReturnValue, AllowMultiple=false)] public ref class XmlAnyAttributeAttribute : public Attribute
/** @attribute AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Parameter|AttributeTargets.ReturnValue, AllowMultiple=false) */ public class XmlAnyAttributeAttribute extends Attribute
AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Parameter|AttributeTargets.ReturnValue, AllowMultiple=false) public class XmlAnyAttributeAttribute extends Attribute
Utilizzare XmlAnyAttributeAttribute per contenere dati arbitrari, ad esempio attributi XML, inviati in un documento XML, come nel caso di metadati in un documento.
Applicare la classe XmlAnyAttributeAttribute a un campo che restituisce una matrice di oggetti XmlAttribute o XmlNode. Quando viene chiamato il metodo Deserialize della classe XmlSerializer, tutti gli attributi XML privi di membro corrispondente nella classe da deserializzare vengono raccolti nella matrice. Dopo la deserializzazione è possibile scorrere l'insieme di elementi XmlAttribute per elaborare i dati.
Gli eventi UnknownNode e UnknownAttribute della classe XmlSerializer non si verificano se a un membro di una classe viene applicato XmlAnyAttributeAttribute.
Nota |
|---|
| Nel codice è possibile utilizzare la parola XmlAnyAttribute anziché la forma più estesa XmlAnyAttributeAttribute. |
Per ulteriori informazioni sull'utilizzo degli attributi, vedere Estensione di metadati mediante attributi.
Nell'esempio seguente tutti gli attributi sconosciuti vengono raccolti in una matrice di oggetti XmlAttribute. Per eseguire l'esempio, creare un file denominato UnknownAttributes.xml contenente l'XML riportato di seguito:
<?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>
Imports System 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
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(); } }
#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" ); }
import System.*;
import System.IO.*;
import System.Xml.Serialization.*;
import System.Xml.*;
public class Group
{
public String groupName;
// The UnknownAttributes array will be used to collect all unknown
// attributes found when deserializing.
/** @attribute XmlAnyAttribute()
*/
public XmlAttribute xAttributes[];
} //Group
public class Test
{
public static void main(String[] args)
{
Test t = new Test();
// Deserialize the file containing unknown attributes.
t.DeserializeObject("UnknownAttributes.xml");
} //main
private void DeserializeObject(String fileName)
{
XmlSerializer ser = new XmlSerializer(Group.class.ToType());
// 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.get_Length());
for (int iCtr = 0; iCtr < g.xAttributes.get_Count(); iCtr++) {
XmlAttribute xAtt = (XmlAttribute)g.xAttributes.get_Item(iCtr);
Console.WriteLine(xAtt.get_Name() + ": " + xAtt.get_InnerXml());
}
// Serialize the object again with the attributes added.
this.SerializeObject("AttributesAdded.xml", g);
} //DeserializeObject
private void SerializeObject(String fileName, Object g)
{
XmlSerializer ser = new XmlSerializer(Group.class.ToType());
TextWriter writer = new StreamWriter(fileName);
ser.Serialize(writer, g);
writer.Close();
} //SerializeObject
} //Test
System.Attribute
System.Xml.Serialization.XmlAnyAttributeAttribute
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile per Pocket PC, Windows Mobile per Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework non supporta tutte le versioni di ciascuna piattaforma. Per un elenco delle versioni supportate, vedere Requisiti di sistema.
.NET Framework
Supportato in: 2.0 1.1 1.0.NET Compact Framework
Supportato in: 2.0 1.0Riferimenti
Membri XmlAnyAttributeAttributeSpazio dei nomi System.Xml.Serialization
XmlAnyElementAttribute
XmlSerializer
XmlAttributes
Altre risorse
Introduzione alla serializzazione XMLProcedura: specificare un nome di elemento alternativo per un flusso XML
Controllo della serializzazione XML mediante attributi
Esempi di serializzazione XML
Strumento di definizione di schemi XML (Xsd.exe)
Nota