XmlAnyAttributeAttribute クラス
アセンブリ: System.Xml (system.xml.dll 内)
構文
<AttributeUsageAttribute(AttributeTargets.Property Or AttributeTargets.Field Or AttributeTargets.Parameter Or AttributeTargets.ReturnValue, AllowMultiple:=False)> _ Public Class XmlAnyAttributeAttribute Inherits Attribute
[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
XML ドキュメントに含まれるメタデータなど、XML ドキュメントの一部として送信される可能性がある任意のデータを XML 属性として含めるには、XmlAnyAttributeAttribute を使用します。
XmlAnyAttributeAttribute は、XmlAttribute オブジェクトまたは XmlNode オブジェクトの配列を返すフィールドに適用します。XmlSerializer クラスの Deserialize メソッドが呼び出されると、逆シリアル化対象のクラス内に対応するメンバがない XML 属性はすべて、配列にまとめられます。逆シリアル化が終了したら、XmlAttribute 項目のコレクションを反復処理して、データを処理できます。
XmlAnyAttributeAttribute をクラスのメンバに適用すると、XmlSerializer の UnknownNode イベントと UnknownAttribute イベントは発生しなくなります。
メモ |
---|
コードでは、XmlAnyAttributeAttribute の代わりに XmlAnyAttribute という短い語を使用できます。 |
不明な属性をすべて XmlAttribute オブジェクトの配列にまとめる例を次に示します。このサンプル コードを実行するには、次の XML を含んだ UnknownAttributes.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>
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 for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
- XmlAnyAttributeAttribute クラスのページへのリンク