XmlArrayAttribute.Form 속성
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
XmlSerializer에서 생성한 XML 요소 이름이 정규화되었는지 여부를 나타내는 값을 가져오거나 설정합니다.
public:
property System::Xml::Schema::XmlSchemaForm Form { System::Xml::Schema::XmlSchemaForm get(); void set(System::Xml::Schema::XmlSchemaForm value); };
public System.Xml.Schema.XmlSchemaForm Form { get; set; }
member this.Form : System.Xml.Schema.XmlSchemaForm with get, set
Public Property Form As XmlSchemaForm
속성 값
XmlSchemaForm 값 중 하나입니다. 기본값은 XmlSchemaForm.None
입니다.
예제
다음 예제에서는 클래스의 인스턴스를 직렬화합니다 Enterprises
. 두 XML 요소의 로컬 이름(Company
)은 같지만 접두사는 다릅니다. 이 예제에서는 정규화된 이름이 XML 인스턴스에서 발생하도록 속성을 로 설정합니다 FormXmlForm.Qualified
.
#using <System.Xml.dll>
#using <System.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Schema;
using namespace System::Xml::Serialization;
public ref class Winery
{
public:
String^ Name;
};
public ref class VacationCompany
{
public:
String^ Name;
};
public ref class Enterprises
{
private:
array<Winery^>^wineries;
array<VacationCompany^>^companies;
public:
// Sets the Form property to qualified, and specifies the namespace.
[XmlArray(Form=XmlSchemaForm::Qualified,ElementName="Company",
Namespace="http://www.cohowinery.com")]
property array<Winery^>^ Wineries
{
array<Winery^>^ get()
{
return wineries;
}
void set( array<Winery^>^value )
{
wineries = value;
}
}
[XmlArray(Form=XmlSchemaForm::Qualified,ElementName="Company",
Namespace="http://www.treyresearch.com")]
property array<VacationCompany^>^ Companies
{
array<VacationCompany^>^ get()
{
return companies;
}
void set( array<VacationCompany^>^value )
{
companies = value;
}
}
};
int main()
{
String^ filename = "MyEnterprises.xml";
// Creates an instance of the XmlSerializer class.
XmlSerializer^ mySerializer = gcnew XmlSerializer( Enterprises::typeid );
// Writing file requires a TextWriter.
TextWriter^ writer = gcnew StreamWriter( filename );
// Creates an instance of the XmlSerializerNamespaces class.
XmlSerializerNamespaces^ ns = gcnew XmlSerializerNamespaces;
// Adds namespaces and prefixes for the XML document instance.
ns->Add( "winery", "http://www.cohowinery.com" );
ns->Add( "vacationCompany", "http://www.treyresearch.com" );
// Creates an instance of the class that will be serialized.
Enterprises^ myEnterprises = gcnew Enterprises;
// Creates objects and adds to the array.
Winery^ w1 = gcnew Winery;
w1->Name = "cohowinery";
array<Winery^>^myWinery = {w1};
myEnterprises->Wineries = myWinery;
VacationCompany^ com1 = gcnew VacationCompany;
com1->Name = "adventure-works";
array<VacationCompany^>^myCompany = {com1};
myEnterprises->Companies = myCompany;
// Serializes the class, and closes the TextWriter.
mySerializer->Serialize( writer, myEnterprises, ns );
writer->Close();
}
void ReadEnterprises( String^ filename )
{
XmlSerializer^ mySerializer = gcnew XmlSerializer( Enterprises::typeid );
FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
Enterprises^ myEnterprises = dynamic_cast<Enterprises^>(mySerializer->Deserialize( fs ));
for ( int i = 0; i < myEnterprises->Wineries->Length; i++ )
{
Console::WriteLine( myEnterprises->Wineries[ i ]->Name );
}
for ( int i = 0; i < myEnterprises->Companies->Length; i++ )
{
Console::WriteLine( myEnterprises->Companies[ i ]->Name );
}
}
using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
public class Enterprises
{
private Winery[] wineries;
private VacationCompany[] companies;
// Sets the Form property to qualified, and specifies the namespace.
[XmlArray(Form = XmlSchemaForm.Qualified, ElementName="Company",
Namespace="http://www.cohowinery.com")]
public Winery[] Wineries{
get{return wineries;}
set{wineries = value;}
}
[XmlArray(Form = XmlSchemaForm.Qualified, ElementName = "Company",
Namespace = "http://www.treyresearch.com")]
public VacationCompany [] Companies{
get{return companies;}
set{companies = value;}
}
}
public class Winery
{
public string Name;
}
public class VacationCompany{
public string Name;
}
public class Run
{
public static void Main()
{
Run test = new Run();
test.WriteEnterprises("MyEnterprises.xml");
}
public void WriteEnterprises(string filename)
{
// Creates an instance of the XmlSerializer class.
XmlSerializer mySerializer =
new XmlSerializer(typeof(Enterprises));
// Writing file requires a TextWriter.
TextWriter writer = new StreamWriter(filename);
// Creates an instance of the XmlSerializerNamespaces class.
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
// Adds namespaces and prefixes for the XML document instance.
ns.Add("winery", "http://www.cohowinery.com");
ns.Add("vacationCompany", "http://www.treyresearch.com");
// Creates an instance of the class that will be serialized.
Enterprises myEnterprises = new Enterprises();
// Creates objects and adds to the array.
Winery w1= new Winery();
w1.Name = "cohowinery";
Winery[]myWinery = {w1};
myEnterprises.Wineries = myWinery;
VacationCompany com1 = new VacationCompany();
com1.Name = "adventure-works";
VacationCompany[] myCompany = {com1};
myEnterprises.Companies = myCompany;
// Serializes the class, and closes the TextWriter.
mySerializer.Serialize(writer, myEnterprises, ns);
writer.Close();
}
public void ReadEnterprises(string filename)
{
XmlSerializer mySerializer =
new XmlSerializer(typeof(Enterprises));
FileStream fs = new FileStream(filename, FileMode.Open);
Enterprises myEnterprises = (Enterprises)
mySerializer.Deserialize(fs);
for(int i = 0; i < myEnterprises.Wineries.Length;i++)
{
Console.WriteLine(myEnterprises.Wineries[i].Name);
}
for(int i = 0; i < myEnterprises.Companies.Length;i++)
{
Console.WriteLine(myEnterprises.Companies[i].Name);
}
}
}
Imports System.IO
Imports System.Xml
Imports System.Xml.Schema
Imports System.Xml.Serialization
Public Class Enterprises
Private mywineries() As Winery
Private mycompanies() As VacationCompany
' Sets the Form property to qualified, and specifies the Namespace.
<XmlArray(Form := XmlSchemaForm.Qualified, _
ElementName := "Company", _
Namespace := "http://www.cohowinery.com")> _
Public Property Wineries() As Winery()
Get
Return mywineries
End Get
Set
mywineries = value
End Set
End Property
<XmlArray(Form := XmlSchemaForm.Qualified, _
ElementName := "Company", _
Namespace := "http://www.treyresearch.com")> _
Public Property Companies() As VacationCompany()
Get
Return mycompanies
End Get
Set
mycompanies = value
End Set
End Property
End Class
Public Class Winery
Public Name As String
End Class
Public Class VacationCompany
Public Name As String
End Class
Public Class Run
Public Shared Sub Main()
Dim test As New Run()
test.WriteEnterprises("MyEnterprises.xml")
End Sub
Public Sub WriteEnterprises(filename As String)
' Creates an instance of the XmlSerializer class.
Dim mySerializer As New XmlSerializer(GetType(Enterprises))
' Writing a file requires a TextWriter.
Dim writer As New StreamWriter(filename)
' Creates an instance of the XmlSerializerNamespaces class.
Dim ns As New XmlSerializerNamespaces()
' Adds namespaces and prefixes for the XML document instance.
ns.Add("winery", "http://www.cohowinery.com")
ns.Add("vacationCompany", "http://www.treyresearch.com")
' Creates an instance of the class that will be serialized.
Dim myEnterprises As New Enterprises()
' Creates objects and adds to the array.
Dim w1 As New Winery()
w1.Name = "cohowinery"
Dim myWinery(0) As Winery
myWinery(0) = w1
myEnterprises.Wineries = myWinery
Dim com1 As New VacationCompany()
com1.Name = "adventure-works"
Dim myCompany(0) As VacationCompany
myCompany(0) = com1
myEnterprises.Companies = myCompany
' Serializes the class, and closes the TextWriter.
mySerializer.Serialize(writer, myEnterprises, ns)
writer.Close()
End Sub
Public Sub ReadEnterprises(filename As String)
Dim mySerializer As New XmlSerializer(GetType(Enterprises))
Dim fs As New FileStream(filename, FileMode.Open)
Dim myEnterprises As Enterprises = CType(mySerializer.Deserialize(fs), Enterprises)
Dim i As Integer
For i = 0 To myEnterprises.Wineries.Length - 1
Console.WriteLine(myEnterprises.Wineries(i).Name)
Next i
For i = 0 To myEnterprises.Companies.Length - 1
Console.WriteLine(myEnterprises.Companies(i).Name)
Next i
End Sub
End Class
설명
속성은 Form XML 요소 이름이 정규화되었는지 아니면 정규화되지 않은지 여부를 결정합니다. 속성은 FormXML의 네임스페이스라는 1999년 World Wide Web 컨소시엄 문서를 준수합니다.
속성이 Namespace 임의의 값으로 설정된 경우 속성을 XmlSchemaForm.Unqualified
설정 Form 하려고 하면 예외가 throw됩니다.
기본 설정인 XmlSchemaForm.None
는 에 XML 문서의 스키마를 확인하여 네임스페이스가 정규화되었는지 여부를 확인하도록 지시 XmlSerializer 합니다. 스키마가 개별 요소 또는 특성에 대한 값을 지정하지 않으면 는 XmlSerializer 및 attributeFormDefault
값을 사용하여 elementFormDefault
요소 또는 특성의 정규화 여부를 결정합니다. 다음 XML 코드는 스키마를 보여줍니다.
<schema elementFormDefault="qualified"
attributeFormDefault="unqualified"... >
<element name="Name"/>
<attribute name="Number"/>
</schema>
가 XmlSerializer 스키마 Form 를 읽을 때 및 Number
의 값 Name
은 XmlSchemaForm.None
이지만 Name
요소는 정규화 Number
되고 요소는 정규화되지 않습니다.
적용 대상
추가 정보
.NET