XmlAnyElementAttribute.Name プロパティ
XML 要素名を取得または設定します。
Public Property Name As String
[C#]
public string Name {get; set;}
[C++]
public: __property String* get_Name();public: __property void set_Name(String*);
[JScript]
public function get Name() : String;public function set Name(String);
プロパティ値
XML 要素の名前。
例外
例外の種類 | 条件 |
---|---|
InvalidOperationException | 配列メンバの要素名が、 Name プロパティに指定されている要素名と一致しません。 |
解説
この属性を適用するときに Name プロパティ値を指定した場合は、配列に挿入されるすべての XmlElement オブジェクトまたは XmlNode オブジェクトは、指定された同じ要素名と、同じ既定の名前空間を持っていることが必要です。それ以外の場合は、例外がスローされます。 Namespace プロパティ値を設定した場合は、同様に Name プロパティも設定する必要があり、 XmlElement オブジェクトまたは XmlNode オブジェクトの名前と名前空間の値がすべて同じであることが必要です。 Name 値を指定しない場合は、任意の名前を持つ XmlElement オブジェクトまたは XmlNode オブジェクトを配列に含めることができます。
XmlSerializer クラスの Deserialize メソッドが呼び出されると、逆シリアル化対象のオブジェクト内に対応するメンバがない属性はすべて、配列にまとめられます。 Name 値を指定すると、配列には、指定した名前の XML 要素だけが格納されます。 Name 値を指定しない場合は、配列には、クラス内に対応するメンバがない要素がすべて格納されます。クラスに含まれている複数のフィールドにこの属性を適用する場合は、 Name プロパティと Namespace プロパティを使用して、各配列の内容を区別できるようにする必要があります。このようなクラス (複数のフィールドが含まれている) に、配列を区別するためのプロパティ値 (つまり、 Name と Namespace) が設定されていないフィールドが 1 つ含まれている場合、逆シリアル化時に、この配列には、他の配列には格納されない不明な XML 要素が格納されます。配列を区別するための Name 値または Namespace 値が設定されていない複数のフィールドを追加した場合は、クラスの最後のフィールドに、他の配列には格納されない不明な要素が格納され、その他のフィールドはすべて null 参照 (Visual Basic では Nothing) に設定されます。
XmlAnyElementAttribute の複数のインスタンスを 1 つのクラス メンバに適用できますが、各インスタンスには個別の Name プロパティ値を設定する必要があります。また、各インスタンスに同じ Name プロパティを設定した場合は、各インスタンスに設定する Namespace プロパティ値は個別にする必要があります。
使用例
Imports System
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
[C#]
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);}
}
}
[C++]
#using <mscorlib.dll>
#using <system.dll>
#using <system.xml.dll>
using namespace System;
using namespace System::Text;
using namespace System::IO;
using namespace System::Xml::Serialization;
using namespace System::Xml;
using namespace System::Xml::Schema;
[XmlRoot(Namespace = "http://www.cohowinery.com")]
public __gc class Group
{
public:
String* GroupName;
// This is for serializing Employee elements.
[XmlAnyElement(Name = "Employee")]
XmlElement* UnknownEmployees[];
// This is for serializing City elements.
[XmlAnyElement
(Name = "City",
Namespace = "http://www.cpandl.com")]
XmlElement* UnknownCity[];
// This one is for all other unknown elements.
[XmlAnyElement]
XmlElement* UnknownElements[];
};
void SerializeObject(String* filename)
{
XmlSerializer* ser = new XmlSerializer(__typeof(Group));
// Create an XmlNamespaces to use.
XmlSerializerNamespaces* namespaces =
new XmlSerializerNamespaces();
namespaces->Add(S"c", S"http://www.cohowinery.com");
namespaces->Add(S"i", S"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(S"Employee", S"http://www.cohowinery.com");
El1->InnerText = S"John";
XmlElement* El2 = xDoc->CreateElement(S"Employee", S"http://www.cohowinery.com");
El2->InnerText = S"Joan";
XmlElement* El3 = xDoc->CreateElement(S"Employee", S"http://www.cohowinery.com");
El3->InnerText = S"Jim";
XmlElement* employees[] = { El1, El2, El3 };
myGroup->UnknownEmployees= employees;
// Create an array of City XmlElement objects.
XmlElement* inf1 = xDoc->CreateElement(S"City", S"http://www.cpandl.com");
inf1->InnerText = S"Tokyo";
XmlElement* inf2 = xDoc->CreateElement(S"City", S"http://www.cpandl.com");
inf2->InnerText = S"New York";
XmlElement* inf3 = xDoc->CreateElement(S"City", S"http://www.cpandl.com");
inf3->InnerText = S"Rome";
XmlElement* cities[] = { inf1, inf2, inf3 };
myGroup->UnknownCity = cities;
XmlElement* xEl1 = xDoc->CreateElement(S"bld");
xEl1->InnerText = S"42";
XmlElement* xEl2 = xDoc->CreateElement(S"Region");
xEl2->InnerText = S"West";
XmlElement* xEl3 = xDoc->CreateElement(S"type");
xEl3->InnerText = S"Technical";
XmlElement* elements[] = { xEl1, xEl2, xEl3 };
myGroup->UnknownElements = elements;
// Serialize the class, and close the TextWriter.
TextWriter* writer = new StreamWriter(filename);
ser->Serialize(writer, myGroup, namespaces);
writer->Close();
}
void DeserializeObject(String* filename)
{
XmlSerializer* ser = new XmlSerializer(__typeof(Group));
FileStream* fs = new FileStream(filename, FileMode::Open);
Group* myGroup;
myGroup = __try_cast<Group*>( ser->Deserialize(fs));
fs->Close();
for (int i = 0; i < myGroup->UnknownEmployees->Length; ++i )
{
XmlElement* xEmp = myGroup->UnknownEmployees[i];
Console::WriteLine( S"{0}: {1}", xEmp->LocalName, xEmp->InnerText );
}
for (int i = 0; i < myGroup->UnknownCity->Length; ++i )
{
XmlElement* xCity = myGroup->UnknownCity[i];
Console::WriteLine( S"{0}: {1}", xCity->LocalName, xCity->InnerText );
}
for (int i = 0; i < myGroup->UnknownElements->Length; ++i )
{
XmlElement* xEmp = myGroup->UnknownElements[i];
Console::WriteLine( S"{0}: {1}", xEmp->LocalName, xEmp->InnerText );
}
}
int main()
{
SerializeObject("AnyElementArray.xml");
DeserializeObject("AnyElementArray.xml");
Console::WriteLine("Done");
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET
参照
XmlAnyElementAttribute クラス | XmlAnyElementAttribute メンバ | System.Xml.Serialization 名前空間