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但前綴不同。 範例設定 Form 屬性設定為 , XmlForm.Qualified 以確保合格名稱出現在 XML 實例中。
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 元素名稱是否為限定或不限定。 該 Form 物業符合1999年萬維網聯盟(WW)文件《 XML中的命名空間》。
如果屬性 Namespace 設定為任意值,嘗試將屬性 Form 設為 會 XmlSchemaForm.Unqualified 拋出例外。
預設設定 XmlSchemaForm.None,指示 檢查 XmlSerializer XML 文件的結構,以判斷命名空間是否符合資格。 若結構未指定個別元素或屬性的值,則 XmlSerializer 使用 和 elementFormDefaultattributeFormDefault 值來判斷該元素或屬性是否被限定。 以下 XML 程式碼顯示了一個結構架構:
<schema elementFormDefault="qualified"
attributeFormDefault="unqualified"... >
<element name="Name"/>
<attribute name="Number"/>
</schema>
當 讀取 XmlSerializer schema 時,Form與 Number 的值皆Name為 XmlSchemaForm.None,但Name元素是限定的,而Number元素是非限定的。