XmlElementAttributes 클래스
XmlSerializer가 클래스를 serialize하는 기본 방식을 재정의하는 데 사용하는 XmlElementAttribute 개체의 컬렉션을 나타냅니다.
네임스페이스: System.Xml.Serialization
어셈블리: System.Xml(system.xml.dll)
‘선언
Public Class XmlElementAttributes
Inherits CollectionBase
‘사용 방법
Dim instance As XmlElementAttributes
public class XmlElementAttributes : CollectionBase
public ref class XmlElementAttributes : public CollectionBase
public class XmlElementAttributes extends CollectionBase
public class XmlElementAttributes extends CollectionBase
XmlElementAttributes은 XmlAttributes 클래스의 XmlElements 속성에 의해 반환됩니다. XmlAttributeOverrides 클래스 및 XmlAttributes 클래스를 사용하여 XmlSerializer가 클래스를 serialize하는 기본 방식을 재정의할 수 있습니다.
다음 예제에서는 ArrayList를 반환하는 단일 필드 Vehicles를 포함하는 Transportation 클래스를 serialize합니다. 이 예제에서는 우선 XmlElementAttribute 클래스의 두 개의 인스턴스를 XmlSerializer가 배열에 삽입하는 개체의 형식을 지정하는 Vehicles 필드에 적용합니다. 그런 다음 두 개의 XmlElementAttribute 개체를 만들어 Vehicles 속성에 적용된 특성의 동작을 재정의합니다. 이 두 개의 재정의 개체는 XmlAttributes의 XmlElementAttributes 컬렉션에 추가됩니다. 마지막으로 XmlAttributes를 XmlAttributeOverrides에 추가하며 XmlSerializer가 새 개체 형식을 Vehicles 필드에 의해 반환되는 ArrayList에 삽입할 수 있도록 합니다.
Imports System
Imports System.IO
Imports System.Xml.Serialization
Imports System.Collections
Imports System.Xml
Public Class Transportation
' Override these two XmlElementAttributes.
<XmlElement(GetType(Car)), _
XmlElement(GetType(Plane))> _
Public Vehicles As ArrayList
End Class
Public Class Car
Public Name As String
End Class
Public Class Plane
Public Name As String
End Class
Public Class Truck
Public Name As String
End Class
Public Class Train
Public Name As String
End Class
Public Class Test
Public Shared Sub Main()
Dim t As New Test()
t.SerializeObject("OverrideElement.xml")
End Sub
Public Function CreateOverrider() As XmlSerializer
' Create XmlAtrributes and XmlAttributeOverrides instances.
Dim attrs As New XmlAttributes()
Dim xOver As New XmlAttributeOverrides()
' Create an XmlElementAttributes object to override
' one of the attributes applied to the Vehicles property.
Dim xElement1 As New XmlElementAttribute(GetType(Truck))
' Add the XmlElementAttribute to the collection.
attrs.XmlElements.Add(xElement1)
' Create a second XmlElementAttribute and
' add it to the collection.
Dim xElement2 As New XmlElementAttribute(GetType(Train))
attrs.XmlElements.Add(xElement2)
' Add the XmlAttributes to the XmlAttributeOverrides,
' specifying the member to override.
xOver.Add(GetType(Transportation), "Vehicles", attrs)
' Create the XmlSerializer, and return it.
Dim xSer As New XmlSerializer(GetType(Transportation), xOver)
Return xSer
End Function
Public Sub SerializeObject(ByVal filename As String)
' Create an XmlSerializer instance.
Dim xSer As XmlSerializer = CreateOverrider()
' Create the object.
Dim myTransportation As New Transportation()
' Create two new, overriding objects that can be
' inserted into the Vehicles array.
myTransportation.Vehicles = New ArrayList()
Dim myTruck As New Truck()
myTruck.Name = "MyTruck"
Dim myTrain As New Train()
myTrain.Name = "MyTrain"
myTransportation.Vehicles.Add(myTruck)
myTransportation.Vehicles.Add(myTrain)
Dim writer As New StreamWriter(filename)
xSer.Serialize(writer, myTransportation)
End Sub
End Class
using System;
using System.IO;
using System.Xml.Serialization;
using System.Collections;
using System.Xml;
public class Transportation
{
// Override these two XmlElementAttributes.
[XmlElement(typeof(Car)),
XmlElement(typeof(Plane))]
public ArrayList Vehicles;
}
public class Car
{
public string Name;
}
public class Plane
{
public string Name;
}
public class Truck
{
public string Name;
}
public class Train
{
public string Name;
}
public class Test
{
public static void Main()
{
Test t = new Test();
t.SerializeObject("OverrideElement.xml");
}
public XmlSerializer CreateOverrider()
{
// Create XmlAtrributes and XmlAttributeOverrides instances.
XmlAttributes attrs = new XmlAttributes();
XmlAttributeOverrides xOver =
new XmlAttributeOverrides();
/* Create an XmlElementAttributes object to override
one of the attributes applied to the Vehicles property. */
XmlElementAttribute xElement1 =
new XmlElementAttribute(typeof(Truck));
// Add the XmlElementAttribute to the collection.
attrs.XmlElements.Add(xElement1);
/* Create a second XmlElementAttribute and
add it to the collection. */
XmlElementAttribute xElement2 =
new XmlElementAttribute(typeof(Train));
attrs.XmlElements.Add(xElement2);
/* Add the XmlAttributes to the XmlAttributeOverrides,
specifying the member to override. */
xOver.Add(typeof(Transportation), "Vehicles", attrs);
// Create the XmlSerializer, and return it.
XmlSerializer xSer = new XmlSerializer
(typeof(Transportation), xOver);
return xSer;
}
public void SerializeObject(string filename)
{
// Create an XmlSerializer instance.
XmlSerializer xSer = CreateOverrider();
// Create the object.
Transportation myTransportation =
new Transportation();
/* Create two new, overriding objects that can be
inserted into the Vehicles array. */
myTransportation.Vehicles = new ArrayList();
Truck myTruck = new Truck();
myTruck.Name = "MyTruck";
Train myTrain = new Train();
myTrain.Name = "MyTrain";
myTransportation.Vehicles.Add(myTruck);
myTransportation.Vehicles.Add(myTrain);
TextWriter writer = new StreamWriter(filename);
xSer.Serialize(writer, myTransportation);
}
}
#using <System.Xml.dll>
#using <System.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml::Serialization;
using namespace System::Collections;
using namespace System::Xml;
public ref class Car
{
public:
String^ Name;
};
public ref class Plane
{
public:
String^ Name;
};
public ref class Truck
{
public:
String^ Name;
};
public ref class Train
{
public:
String^ Name;
};
public ref class Transportation
{
public:
// Override these two XmlElementAttributes.
[XmlElement(Car::typeid),
XmlElement(Plane::typeid)]
ArrayList^ Vehicles;
};
XmlSerializer^ CreateOverrider()
{
// Create XmlAtrributes and XmlAttributeOverrides instances.
XmlAttributes^ attrs = gcnew XmlAttributes;
XmlAttributeOverrides^ xOver = gcnew XmlAttributeOverrides;
/* Create an XmlElementAttributes object to override
one of the attributes applied to the Vehicles property. */
XmlElementAttribute^ xElement1 = gcnew XmlElementAttribute( Truck::typeid );
// Add the XmlElementAttribute to the collection.
attrs->XmlElements->Add( xElement1 );
/* Create a second XmlElementAttribute and
add it to the collection. */
XmlElementAttribute^ xElement2 = gcnew XmlElementAttribute( Train::typeid );
attrs->XmlElements->Add( xElement2 );
/* Add the XmlAttributes to the XmlAttributeOverrides,
specifying the member to override. */
xOver->Add( Transportation::typeid, "Vehicles", attrs );
// Create the XmlSerializer, and return it.
XmlSerializer^ xSer = gcnew XmlSerializer( Transportation::typeid,xOver );
return xSer;
}
void SerializeObject( String^ filename )
{
// Create an XmlSerializer instance.
XmlSerializer^ xSer = CreateOverrider();
// Create the object.
Transportation^ myTransportation = gcnew Transportation;
/* Create two new, overriding objects that can be
inserted into the Vehicles array. */
myTransportation->Vehicles = gcnew ArrayList;
Truck^ myTruck = gcnew Truck;
myTruck->Name = "MyTruck";
Train^ myTrain = gcnew Train;
myTrain->Name = "MyTrain";
myTransportation->Vehicles->Add( myTruck );
myTransportation->Vehicles->Add( myTrain );
TextWriter^ writer = gcnew StreamWriter( filename );
xSer->Serialize( writer, myTransportation );
}
int main()
{
SerializeObject( "OverrideElement.xml" );
}
import System.*;
import System.IO.*;
import System.Xml.Serialization.*;
import System.Collections.*;
import System.Xml.*;
public class Transportation
{
// Override these two XmlElementAttributes.
/** @attribute XmlElement(Car.class)
@attribute XmlElement(Plane.class)
*/
public ArrayList vehicles;
} //Transportation
public class Car
{
public String name;
} //Car
public class Plane
{
public String name;
} //Plane
public class Truck
{
public String name;
} //Truck
public class Train
{
public String name;
} //Train
public class Test
{
public static void main(String[] args)
{
Test t = new Test();
t.SerializeObject("OverrideElement.xml");
} //main
public XmlSerializer CreateOverrider()
{
// Create XmlAtrributes and XmlAttributeOverrides instances.
XmlAttributes attrs = new XmlAttributes();
XmlAttributeOverrides xOver = new XmlAttributeOverrides();
/* Create an XmlElementAttributes object to override
one of the attributes applied to the Vehicles property. */
XmlElementAttribute xElement1 =
new XmlElementAttribute(Truck.class.ToType());
// Add the XmlElementAttribute to the collection.
attrs.get_XmlElements().Add(xElement1);
/* Create a second XmlElementAttribute and
add it to the collection. */
XmlElementAttribute xElement2 =
new XmlElementAttribute(Train.class.ToType());
attrs.get_XmlElements().Add(xElement2);
/* Add the XmlAttributes to the XmlAttributeOverrides,
specifying the member to override. */
xOver.Add(Transportation.class.ToType(), "vehicles", attrs);
// Create the XmlSerializer, and return it.
XmlSerializer xSer =
new XmlSerializer(Transportation.class.ToType(), xOver);
return xSer;
} //CreateOverrider
public void SerializeObject(String fileName)
{
// Create an XmlSerializer instance.
XmlSerializer xSer = CreateOverrider();
// Create the object.
Transportation myTransportation = new Transportation();
/* Create two new, overriding objects that can be
inserted into the Vehicles array. */
myTransportation.vehicles = new ArrayList();
Truck myTruck = new Truck();
myTruck.name = "MyTruck";
Train myTrain = new Train();
myTrain.name = "MyTrain";
myTransportation.vehicles.Add(myTruck);
myTransportation.vehicles.Add(myTrain);
TextWriter writer = new StreamWriter(fileName);
xSer.Serialize(writer, myTransportation);
} //SerializeObject
} //Test
System.Object
System.Collections.CollectionBase
System.Xml.Serialization.XmlElementAttributes
이 형식의 모든 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에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.
2.0, 1.1, 1.0에서 지원
2.0에서 지원
XmlElementAttributes 멤버
System.Xml.Serialization 네임스페이스
XmlAttributes 클래스
XML Serialization 소개
방법: XML 스트림의 대체 요소 이름 지정
특성을 사용하여 XML Serialization 제어
XML Serialization 예
XML 스키마 정의 도구(Xsd.exe)