XmlAnyAttributeAttribute Třída

Definice

Určuje, že člen (pole, které vrací pole XmlAttribute objektů) může obsahovat jakékoli atributy XML.

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
Dědičnost
XmlAnyAttributeAttribute
Atributy

Příklady

Následující příklad shromažďuje všechny neznámé atributy do pole XmlAttribute objektů. Pokud chcete vyzkoušet příklad, vytvořte soubor s názvem UnknownAttributes.xml , který obsahuje následující KÓD XML:

<?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

Poznámky

XmlAnyAttributeAttribute Použijte k tomu, aby obsahovala libovolná data (jako atributy XML), která se odesílají jako součást dokumentu XML, například metadata odeslaná jako součást dokumentu.

Použijte u XmlAnyAttributeAttribute pole, které vrací pole XmlAttribute nebo XmlNode objekty. Deserialize Když je volána metoda XmlSerializer třídy, všechny atributy XML, které nemají odpovídající člen v deserializované třídě jsou shromažďovány v poli. Po deserializaci můžete iterovat kolekci XmlAttribute položek a zpracovávat data.

K UnknownNode událostem a UnknownAttribute nedojde, XmlSerializer pokud použijete XmlAnyAttributeAttribute na člena třídy.

Poznámka

Místo delšího XmlAnyAttributeAttributeslova můžete v kódu použít slovo XmlAnyAttribute .

Další informace o používání atributů najdete v tématu Atributy.

Konstruktory

XmlAnyAttributeAttribute()

Vytvoří novou instanci XmlAnyAttributeAttribute třídy.

Vlastnosti

TypeId

Při implementaci v odvozené třídě získá jedinečný identifikátor pro tuto Attributetřídu .

(Zděděno od Attribute)

Metody

Equals(Object)

Vrací hodnotu, která určuje, zda je tato instance rovna zadanému objektu.

(Zděděno od Attribute)
GetHashCode()

Vrátí hodnotu hash pro tuto instanci.

(Zděděno od Attribute)
GetType()

Získá aktuální Type instanci.

(Zděděno od Object)
IsDefaultAttribute()

Při přepsání v odvozené třídě označuje, zda je hodnota této instance výchozí hodnotou pro odvozenou třídu.

(Zděděno od Attribute)
Match(Object)

Při přepsání v odvozené třídě vrátí hodnotu, která označuje, zda se tato instance rovná zadanému objektu.

(Zděděno od Attribute)
MemberwiseClone()

Vytvoří mělkou kopii aktuálního Objectsouboru .

(Zděděno od Object)
ToString()

Vrátí řetězec, který představuje aktuální objekt.

(Zděděno od Object)

Explicitní implementace rozhraní

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

Mapuje sadu názvů na odpovídající sadu identifikátorů pro rozesílání.

(Zděděno od Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Načte informace o typu objektu, které lze použít k získání informací o typu pro rozhraní.

(Zděděno od Attribute)
_Attribute.GetTypeInfoCount(UInt32)

Získá počet rozhraní typu informací, které objekt poskytuje (0 nebo 1).

(Zděděno od Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Poskytuje přístup k vlastnostem a metodám vystaveným objektem.

(Zděděno od Attribute)

Platí pro

Viz také