XmlAnyElementAttribute.Name Properti
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mendapatkan atau mengatur nama elemen XML.
public:
property System::String ^ Name { System::String ^ get(); void set(System::String ^ value); };
public string Name { get; set; }
member this.Name : string with get, set
Public Property Name As String
Nilai Properti
Nama elemen XML.
Pengecualian
Nama elemen anggota array tidak cocok dengan nama elemen yang ditentukan oleh Name properti .
Contoh
using System;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using System.Xml.Schema;
[XmlRoot(Namespace = "http://www.cohowinery.com")]
public class Group{
public string GroupName;
// This is for serializing Employee elements.
[XmlAnyElement(Name = "Employee")]
public XmlElement[] UnknownEmployees;
// This is for serializing City elements.
[XmlAnyElement
(Name = "City",
Namespace = "http://www.cpandl.com")]
public XmlElement[] UnknownCity;
// This one is for all other unknown elements.
[XmlAnyElement]
public XmlElement[] UnknownElements;
}
public class Test{
static void Main(){
Test t = new Test();
t.SerializeObject("AnyElementArray.xml");
t.DeserializeObject("AnyElementArray.xml");
Console.WriteLine("Done");
}
private void SerializeObject(string filename){
XmlSerializer ser = new XmlSerializer(typeof(Group));
// Create an XmlNamespaces to use.
XmlSerializerNamespaces namespaces =
new XmlSerializerNamespaces();
namespaces.Add("c", "http://www.cohowinery.com");
namespaces.Add("i", "http://www.cpandl.com");
Group myGroup = new Group();
// Create arrays of arbitrary XmlElement objects.
// First create an XmlDocument, used to create the
// XmlElement objects.
XmlDocument xDoc = new XmlDocument();
// Create an array of Employee XmlElement objects.
XmlElement El1 = xDoc.CreateElement("Employee", "http://www.cohowinery.com");
El1.InnerText = "John";
XmlElement El2 = xDoc.CreateElement("Employee", "http://www.cohowinery.com");
El2.InnerText = "Joan";
XmlElement El3 = xDoc.CreateElement("Employee", "http://www.cohowinery.com");
El3.InnerText = "Jim";
myGroup.UnknownEmployees= new XmlElement[]{El1, El2, El3};
// Create an array of City XmlElement objects.
XmlElement inf1 = xDoc.CreateElement("City", "http://www.cpandl.com");
inf1.InnerText = "Tokyo";
XmlElement inf2 = xDoc.CreateElement("City", "http://www.cpandl.com");
inf2.InnerText = "New York";
XmlElement inf3 = xDoc.CreateElement("City", "http://www.cpandl.com");
inf3.InnerText = "Rome";
myGroup.UnknownCity = new XmlElement[]{inf1, inf2, inf3};
XmlElement xEl1 = xDoc.CreateElement("bld");
xEl1.InnerText = "42";
XmlElement xEl2 = xDoc.CreateElement("Region");
xEl2.InnerText = "West";
XmlElement xEl3 = xDoc.CreateElement("type");
xEl3.InnerText = "Technical";
myGroup.UnknownElements =
new XmlElement[]{xEl1,xEl2,xEl3};
// Serialize the class, and close the TextWriter.
TextWriter writer = new StreamWriter(filename);
ser.Serialize(writer, myGroup, namespaces);
writer.Close();
}
private void DeserializeObject(string filename){
XmlSerializer ser = new XmlSerializer(typeof(Group));
FileStream fs = new FileStream(filename, FileMode.Open);
Group myGroup;
myGroup = (Group)ser.Deserialize(fs);
fs.Close();
foreach(XmlElement xEmp in myGroup.UnknownEmployees){
Console.WriteLine(xEmp.LocalName + ": " + xEmp.InnerText);}
foreach(XmlElement xCity in myGroup.UnknownCity){
Console.WriteLine(xCity.LocalName + ": " + xCity.InnerText);}
foreach(XmlElement xEl in myGroup.UnknownElements){
Console.WriteLine(xEl.LocalName + ": " + xEl.InnerText);}
}
}
Imports System.Text
Imports System.IO
Imports System.Xml.Serialization
Imports System.Xml
Imports System.Xml.Schema
<XmlRoot(Namespace:= "http://www.cohowinery.com")> _
Public Class Group
Public GroupName As String
' This is for serializing Employee elements.
<XmlAnyElement(Name:="Employee")> _
Public UnknownEmployees() As XmlElement
' This is for serializing City elements.
<XmlAnyElement _
(Name:="City", _
Namespace:="http://www.cpandl.com")> _
Public UnknownCity() As XmlElement
' This one is for all other unknown elements.
<XmlAnyElement> _
Public UnknownElements() As XmlElement
End Class
Public Class Test
Shared Sub Main()
Dim t As Test = New Test()
t.SerializeObject("AnyElementArray.xml")
t.DeserializeObject("AnyElementArray.xml")
Console.WriteLine("Done")
End Sub
Private Sub SerializeObject(filename As String)
Dim ser As XmlSerializer = _
New XmlSerializer(GetType(Group))
' Create an XmlNamespaces to use.
Dim namespaces As XmlSerializerNamespaces = _
New XmlSerializerNamespaces()
namespaces.Add("c", "http://www.cohowinery.com")
namespaces.Add("i", "http://www.cpandl.com")
Dim myGroup As Group = New Group()
' Create arrays of arbitrary XmlElement objects.
' First create an XmlDocument, used to create the
' XmlElement objects.
Dim xDoc As XmlDocument = New XmlDocument()
' Create an array of Employee XmlElement objects.
Dim El1 As XmlElement = xDoc.CreateElement("Employee", "http://www.cohowinery.com")
El1.InnerText = "John"
Dim El2 As XmlElement = xDoc.CreateElement("Employee", "http://www.cohowinery.com")
El2.InnerText = "Joan"
Dim El3 As XmlElement = xDoc.CreateElement("Employee", "http://www.cohowinery.com")
El3.InnerText = "Jim"
myGroup.UnknownEmployees= New XmlElement(){El1, El2, El3}
' Create an array of City XmlElement objects.
Dim inf1 As XmlElement = xDoc.CreateElement("City", "http://www.cpandl.com")
inf1.InnerText = "Tokyo"
Dim inf2 As XmlElement = xDoc.CreateElement("City", "http://www.cpandl.com")
inf2.InnerText = "New York"
Dim inf3 As XmlElement = xDoc.CreateElement("City", "http://www.cpandl.com")
inf3.InnerText = "Rome"
myGroup.UnknownCity = New XmlElement(){inf1, inf2, inf3}
Dim xEl1 As XmlElement = xDoc.CreateElement("bld")
xEl1.InnerText = "42"
Dim xEl2 As XmlElement = xDoc.CreateElement("Region")
xEl2.InnerText = "West"
Dim xEl3 As XmlElement = xDoc.CreateElement("type")
xEl3.InnerText = "Technical"
myGroup.UnknownElements = _
New XmlElement(){xEl1,xEl2,xEl3}
' Serialize the class, and close the TextWriter.
Dim writer As TextWriter = New StreamWriter(filename)
ser.Serialize(writer, myGroup, namespaces)
writer.Close()
End Sub
Private Sub DeserializeObject(filename As String)
Dim ser As XmlSerializer = _
New XmlSerializer(GetType(Group))
Dim fs As FileStream = New FileStream(filename, FileMode.Open)
Dim myGroup As Group
myGroup = CType(ser.Deserialize(fs), Group)
fs.Close()
Dim xEmp As XmlElement
for each xEmp in myGroup.UnknownEmployees
Console.WriteLine(xEmp.LocalName & ": " & xEmp.InnerText)
Next
Dim xCity As XmlElement
for each xCity in myGroup.UnknownCity
Console.WriteLine(xCity.LocalName & ": " & xCity.InnerText)
Next
Dim xEl As XmlElement
for each xEl in myGroup.UnknownElements
Console.WriteLine(xEl.LocalName & ": " & xEl.InnerText)
Next
End Sub
End Class
Keterangan
Jika Anda menentukan Name nilai properti saat menerapkan atribut, semua XmlElement atau XmlNode objek yang dimasukkan ke dalam array harus memiliki nama elemen dan namespace default yang sama, atau pengecualian dilemparkan. Jika Anda mengatur Namespace nilai properti, Anda juga harus mengatur Name properti , dan XmlElement objek atau XmlNode juga harus memiliki nama dan nilai namespace yang sama. Jika tidak ada Name nilai yang ditentukan, XmlElement objek atau XmlNode dapat memiliki nama elemen apa pun.
Saat Anda memanggil Deserialize metode XmlSerializer kelas, semua atribut yang tidak memiliki anggota yang sesuai dalam objek yang dideserialisasi dikumpulkan dalam array. Jika Anda menentukan Name nilai, array hanya berisi elemen XML dengan nama tersebut. Jika Anda tidak menentukan Name nilai, array berisi semua elemen yang tidak memiliki anggota yang sesuai di kelas . Jika kelas berisi lebih dari satu bidang yang atributnya diterapkan, gunakan Name properti dan Namespace untuk membedakan antara konten array. Jika kelas seperti itu (dengan beberapa bidang) juga berisi satu bidang yang tidak memiliki kumpulan nilai properti pembeda (yaitu, Name dan Namespace) selama deserialisasi, array berisi elemen XML apa pun yang belum terkandung dalam array lain. Jika Anda menambahkan lebih dari satu bidang yang tidak memiliki set pembeda Name atau Namespace nilai, bidang terakhir di kelas berisi semua elemen tidak dikenal yang belum terkandung dalam array lain, dan bidang lain diatur ke null.
Anda dapat menerapkan beberapa instans XmlAnyElementAttribute ke anggota kelas, tetapi setiap instans harus memiliki nilai properti yang berbeda Name . Atau, jika properti yang sama Name diatur untuk setiap instans, nilai properti yang berbeda Namespace harus diatur untuk setiap instans.