다음을 통해 공유


XmlAnyAttributeAttribute 클래스

멤버(XmlAttribute 개체의 배열을 반환하는 필드)가 XML 특성을 포함할 수 있도록 지정합니다.

네임스페이스: System.Xml.Serialization
어셈블리: System.Xml(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

설명

XmlAnyAttributeAttribute를 사용하여, 문서의 일부로 보낸 메타데이터와 같이 XML 문서의 일부로 보낸 임의의 데이터(XML 특성)를 포함합니다.

XmlAnyAttributeAttributeXmlAttribute 또는 XmlNode 개체의 배열을 반환하는 필드에 적용합니다. XmlSerializer 클래스의 Deserialize 메서드를 호출하는 동안, deserialize되고 있는 클래스에 해당 멤버가 없는 모든 XML 특성이 배열에 수집됩니다. deserialization이 끝나면 XmlAttribute 항목의 컬렉션 전체를 반복하여 데이터를 처리할 수 있습니다.

XmlSerializerUnknownNodeUnknownAttribute 이벤트는 XmlAnyAttributeAttribute를 클래스의 멤버에 적용한 경우 발생하지 않습니다.

참고

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.Object
   System.Attribute
    System.Xml.Serialization.XmlAnyAttributeAttribute

스레드로부터의 안전성

이 형식의 모든 public static(Visual Basic의 경우 Shared) 멤버는 스레드로부터 안전합니다. 인터페이스 멤버는 스레드로부터 안전하지 않습니다.

플랫폼

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에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

XmlAnyAttributeAttribute 멤버
System.Xml.Serialization 네임스페이스
XmlAnyElementAttribute
XmlSerializer
XmlAttributes

기타 리소스

XML Serialization 소개
방법: XML 스트림의 대체 요소 이름 지정
특성을 사용하여 XML Serialization 제어
XML Serialization 예
XML 스키마 정의 도구(Xsd.exe)