XmlArrayItemAttribute 클래스
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
XmlSerializer 직렬화된 배열에 배치할 수 있는 파생 형식을 지정하는 특성을 나타냅니다.
public ref class XmlArrayItemAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue, AllowMultiple=true)]
public class XmlArrayItemAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue, AllowMultiple=true)>]
type XmlArrayItemAttribute = class
inherit Attribute
Public Class XmlArrayItemAttribute
Inherits Attribute
- 상속
- 특성
예제
다음 예제에서는 개체 배열 Group 을 반환하는 필드가 포함된 명명 EmployeesEmployee 된 클래스를 serialize합니다. 이 예제에서는 필드에 적용 XmlArrayItemAttribute 하여 기본 클래스() 형식과 파생 클래스 형식Manager(Employee)의 개체를 serialize된 배열에 삽입할 수 있도록 지시 XmlSerializer 합니다.
using System;
using System.IO;
using System.Xml.Serialization;
public class Group
{
/* The XmlArrayItemAttribute allows the XmlSerializer to insert
both the base type (Employee) and derived type (Manager)
into serialized arrays. */
[XmlArrayItem(typeof(Manager)),
XmlArrayItem(typeof(Employee))]
public Employee[] Employees;
/* Use the XmlArrayItemAttribute to specify types allowed
in an array of Object items. */
[XmlArray]
[XmlArrayItem (typeof(int),
ElementName = "MyNumber"),
XmlArrayItem (typeof(string),
ElementName = "MyString"),
XmlArrayItem(typeof(Manager))]
public object [] ExtraInfo;
}
public class Employee
{
public string Name;
}
public class Manager:Employee{
public int Level;
}
public class Run
{
public static void Main()
{
Run test = new Run();
test.SerializeObject("TypeDoc.xml");
test.DeserializeObject("TypeDoc.xml");
}
public void SerializeObject(string filename)
{
// Creates a new XmlSerializer.
XmlSerializer s = new XmlSerializer(typeof(Group));
// Writing the XML file to disk requires a TextWriter.
TextWriter writer = new StreamWriter(filename);
Group group = new Group();
Manager manager = new Manager();
Employee emp1 = new Employee();
Employee emp2 = new Employee();
manager.Name = "Consuela";
manager.Level = 3;
emp1.Name = "Seiko";
emp2.Name = "Martina";
Employee [] emps = new Employee[3]{manager, emp1, emp2};
group.Employees = emps;
// Creates an int and a string and assigns to ExtraInfo.
group.ExtraInfo = new Object[3]{43, "Extra", manager};
// Serializes the object, and closes the StreamWriter.
s.Serialize(writer, group);
writer.Close();
}
public void DeserializeObject(string filename)
{
FileStream fs = new FileStream(filename, FileMode.Open);
XmlSerializer x = new XmlSerializer(typeof(Group));
Group g = (Group) x.Deserialize(fs);
Console.WriteLine("Members:");
foreach(Employee e in g.Employees)
{
Console.WriteLine("\t" + e.Name);
}
}
}
Option Explicit
Option Strict
Imports System.IO
Imports System.Xml.Serialization
Public Class Group
' The XmlArrayItemAttribute allows the XmlSerializer to insert
' both the base type (Employee) and derived type (Manager)
' into serialized arrays.
<XmlArrayItem(GetType(Manager)), _
XmlArrayItem(GetType(Employee))> _
Public Employees() As Employee
' Use the XmlArrayItemAttribute to specify types allowed
' in an array of Object items.
<XmlArray(), _
XmlArrayItem(GetType(Integer), ElementName := "MyNumber"), _
XmlArrayItem(GetType(String), ElementName := "MyString"), _
XmlArrayItem(GetType(Manager))> _
Public ExtraInfo() As Object
End Class
Public Class Employee
Public Name As String
End Class
Public Class Manager
Inherits Employee
Public Level As Integer
End Class
Public Class Run
Public Shared Sub Main()
Dim test As New Run()
test.SerializeObject("TypeDoc.xml")
test.DeserializeObject("TypeDoc.xml")
End Sub
Public Sub SerializeObject(ByVal filename As String)
' Creates a new XmlSerializer.
Dim s As New XmlSerializer(GetType(Group))
' Writing the XML file to disk requires a TextWriter.
Dim writer As New StreamWriter(filename)
Dim group As New Group()
Dim manager As New Manager()
Dim emp1 As New Employee()
Dim emp2 As New Employee()
manager.Name = "Consuela"
manager.Level = 3
emp1.Name = "Seiko"
emp2.Name = "Martina"
Dim emps() As Employee = {manager, emp1, emp2}
group.Employees = emps
' Creates an int and a string and assigns to ExtraInfo.
group.ExtraInfo = New Object() {43, "Extra", manager}
' Serializes the object, and closes the StreamWriter.
s.Serialize(writer, group)
writer.Close()
End Sub
Public Sub DeserializeObject(ByVal filename As String)
Dim fs As New FileStream(filename, FileMode.Open)
Dim x As New XmlSerializer(GetType(Group))
Dim g As Group = CType(x.Deserialize(fs), Group)
Console.WriteLine("Members:")
Dim e As Employee
For Each e In g.Employees
Console.WriteLine(ControlChars.Tab & e.Name)
Next e
End Sub
End Class
설명
개체 XmlArrayItemAttribute 를 직렬화하거나 역직렬화하는 방법을 XmlSerializer 제어하는 특성 패밀리에 속합니다. 유사한 특성의 전체 목록은 XML Serialization을 제어하는 특성을 참조하세요.
배열을 XmlArrayItemAttribute 반환하거나 배열에 대한 액세스를 제공하는 모든 공용 읽기/쓰기 멤버에 적용할 수 있습니다. 예를 들어 개체, 컬렉션, 또는 인터페이스를 구현하는 클래스의 배열을 ArrayList반환하는 필드입니다 IEnumerable .
XmlArrayItemAttribute 다형성을 지원합니다. 즉, 파생 개체를 배열에 추가할 수 있습니다XmlSerializer. 예를 들어 명명된 클래스가 명명 MammalAnimal된 기본 클래스에서 파생되었다고 가정합니다. 또한 명명 MyAnimals 된 클래스에 개체 배열 Animal 을 반환하는 필드가 포함되어 있다고 가정합니다. 형식과 Mammal 형식을 모두 Animal serialize할 수 있도록 XmlSerializer 허용하려면 두 개의 허용 가능한 형식 중 하나를 지정할 때마다 필드에 두 번 적용 XmlArrayItemAttribute 합니다.
메모
여러 인스턴스를 적용하거나 XmlElementAttribute 배열에 XmlArrayItemAttribute 삽입할 수 있는 개체 유형을 지정할 수 있습니다.
메모
인터페이스 또는 인터페이스 배열을 반환하는 필드 또는 속성의 serialization은 지원되지 않습니다.
특성 사용에 대한 자세한 내용은 특성을 참조하세요.
메모
더 긴 XmlArrayItem단어 대신 코드에서 단어를 XmlArrayItemAttribute 사용할 수 있습니다.
생성자
| Name | Description |
|---|---|
| XmlArrayItemAttribute() |
XmlArrayItemAttribute 클래스의 새 인스턴스를 초기화합니다. |
| XmlArrayItemAttribute(String, Type) |
클래스의 XmlArrayItemAttribute 새 인스턴스를 초기화하고 XML 문서에서 생성된 XML 요소의 이름과 생성된 XML 문서에 Type 삽입할 수 있는 XML 요소의 이름을 지정합니다. |
| XmlArrayItemAttribute(String) |
클래스의 새 인스턴스를 XmlArrayItemAttribute 초기화하고 XML 문서에서 생성된 XML 요소의 이름을 지정합니다. |
| XmlArrayItemAttribute(Type) |
클래스의 새 인스턴스를 XmlArrayItemAttribute 초기화하고 serialize된 배열에 삽입할 수 있는 인스턴스를 지정합니다 Type . |
속성
| Name | Description |
|---|---|
| DataType |
생성된 XML 요소의 XML 데이터 형식을 가져오거나 설정합니다. |
| ElementName |
생성된 XML 요소의 이름을 가져오거나 설정합니다. |
| Form |
생성된 XML 요소의 이름이 정규화되었는지 여부를 나타내는 값을 가져오거나 설정합니다. |
| IsNullable |
특성XmlSerializer이 설정된 빈 XML 태그 |
| Namespace |
생성된 XML 요소의 네임스페이스를 가져오거나 설정합니다. |
| NestingLevel |
영향을 주는 XML 요소 XmlArrayItemAttribute 의 계층 구조에서 수준을 가져오거나 설정합니다. |
| Type |
배열에 허용되는 형식을 가져오거나 설정합니다. |
| TypeId |
파생 클래스에서 구현되는 경우 이 Attribute대한 고유 식별자를 가져옵니다. (다음에서 상속됨 Attribute) |
메서드
| Name | Description |
|---|---|
| Equals(Object) |
이 인스턴스가 지정된 개체와 같은지 여부를 나타내는 값을 반환합니다. (다음에서 상속됨 Attribute) |
| GetHashCode() |
이 인스턴스의 해시 코드를 반환합니다. (다음에서 상속됨 Attribute) |
| GetType() |
현재 인스턴스의 Type 가져옵니다. (다음에서 상속됨 Object) |
| IsDefaultAttribute() |
파생 클래스에서 재정의되는 경우 이 인스턴스의 값이 파생 클래스의 기본값인지 여부를 나타냅니다. (다음에서 상속됨 Attribute) |
| Match(Object) |
파생 클래스에서 재정의되는 경우 이 인스턴스가 지정된 개체와 같은지 여부를 나타내는 값을 반환합니다. (다음에서 상속됨 Attribute) |
| MemberwiseClone() |
현재 Object단순 복사본을 만듭니다. (다음에서 상속됨 Object) |
| ToString() |
현재 개체를 나타내는 문자열을 반환합니다. (다음에서 상속됨 Object) |
명시적 인터페이스 구현
| Name | Description |
|---|---|
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
이름 집합을 해당 디스패치 식별자 집합에 매핑합니다. (다음에서 상속됨 Attribute) |
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
인터페이스의 형식 정보를 가져오는 데 사용할 수 있는 개체의 형식 정보를 검색합니다. (다음에서 상속됨 Attribute) |
| _Attribute.GetTypeInfoCount(UInt32) |
개체가 제공하는 형식 정보 인터페이스의 수를 검색합니다(0 또는 1). (다음에서 상속됨 Attribute) |
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
개체에 의해 노출되는 속성 및 메서드에 대한 액세스를 제공합니다. (다음에서 상속됨 Attribute) |