XmlRootAttribute.ElementName プロパティ
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
XmlSerializer クラスの Serialize(TextWriter, Object) メソッドおよび Deserialize(Stream) メソッドによって生成および認識される XML 要素名を取得または設定します。
public:
property System::String ^ ElementName { System::String ^ get(); void set(System::String ^ value); };
public string ElementName { get; set; }
member this.ElementName : string with get, set
Public Property ElementName As String
プロパティ値
XML ドキュメント インスタンスで生成および認識された XML ルート要素名。 既定値は、シリアル化されたクラスの名前です。
例
次の例では、クラスのインスタンスを XmlRootAttribute 作成し、プロパティを ElementName 新しい値に設定します。 オブジェクトは、オブジェクトのシリアル化を XmlAttributeOverrides オーバーライドするために使用されるオブジェクトを作成するために使用されます。
public:
void SerializeOrder( String^ filename )
{
// Create an XmlSerializer instance using the method below.
XmlSerializer^ myXmlSerializer = CreateOverrider();
// Create the object, and set its Name property.
Student^ myStudent = gcnew Student;
myStudent->Name = "Student class1";
// Serialize the class, and close the TextWriter.
TextWriter^ writer = gcnew StreamWriter( filename );
myXmlSerializer->Serialize( writer, myStudent );
writer->Close();
}
// Return an XmlSerializer to the root serialization.
XmlSerializer^ CreateOverrider()
{
// Create an XmlAttributes to the default root element.
XmlAttributes^ myXmlAttributes = gcnew XmlAttributes;
// Create an XmlRootAttribute and set its element name and namespace.
XmlRootAttribute^ myXmlRootAttribute = gcnew XmlRootAttribute;
myXmlRootAttribute->ElementName = "OverriddenRootElementName";
myXmlRootAttribute->Namespace = "http://www.microsoft.com";
// Set the XmlRoot property to the XmlRoot object.
myXmlAttributes->XmlRoot = myXmlRootAttribute;
XmlAttributeOverrides^ myXmlAttributeOverrides =
gcnew XmlAttributeOverrides;
// Add the XmlAttributes object to the XmlAttributeOverrides object.
myXmlAttributeOverrides->Add( Student::typeid, myXmlAttributes );
// Create the Serializer, and return it.
XmlSerializer^ myXmlSerializer = gcnew XmlSerializer(
Student::typeid, myXmlAttributeOverrides );
return myXmlSerializer;
}
public void SerializeOrder(string filename)
{
// Create an XmlSerializer instance using the method below.
XmlSerializer myXmlSerializer = CreateOverrider();
// Create the object, and set its Name property.
Student myStudent = new Student();
myStudent.Name = "Student class1";
// Serialize the class, and close the TextWriter.
TextWriter writer = new StreamWriter(filename);
myXmlSerializer.Serialize(writer, myStudent);
writer.Close();
}
// Return an XmlSerializer to override the root serialization.
public XmlSerializer CreateOverrider()
{
// Create an XmlAttributes to override the default root element.
XmlAttributes myXmlAttributes = new XmlAttributes();
// Create an XmlRootAttribute and set its element name and namespace.
XmlRootAttribute myXmlRootAttribute = new XmlRootAttribute();
myXmlRootAttribute.ElementName = "OverriddenRootElementName";
myXmlRootAttribute.Namespace = "http://www.microsoft.com";
// Set the XmlRoot property to the XmlRoot object.
myXmlAttributes.XmlRoot = myXmlRootAttribute;
XmlAttributeOverrides myXmlAttributeOverrides =
new XmlAttributeOverrides();
/* Add the XmlAttributes object to the
XmlAttributeOverrides object. */
myXmlAttributeOverrides.Add(typeof(Student), myXmlAttributes);
// Create the Serializer, and return it.
XmlSerializer myXmlSerializer = new XmlSerializer
(typeof(Student), myXmlAttributeOverrides);
return myXmlSerializer;
}
Public Sub SerializeOrder(filename As String)
' Create an XmlSerializer instance using the method below.
Dim myXmlSerializer As XmlSerializer = CreateOverrider()
' Create the object, and set its Name property.
Dim myStudent As New Student()
myStudent.Name = "Student class1"
' Serialize the class, and close the TextWriter.
Dim writer = New StreamWriter(filename)
myXmlSerializer.Serialize(writer, myStudent)
writer.Close()
End Sub
' Return an XmlSerializer to override the root serialization.
Public Function CreateOverrider() As XmlSerializer
' Create an XmlAttributes to override the default root element.
Dim myXmlAttributes As New XmlAttributes()
' Create an XmlRootAttribute and set its element name and namespace.
Dim myXmlRootAttribute As New XmlRootAttribute()
myXmlRootAttribute.ElementName = "OverriddenRootElementName"
myXmlRootAttribute.Namespace = "http://www.microsoft.com"
' Set the XmlRoot property to the XmlRoot object.
myXmlAttributes.XmlRoot = myXmlRootAttribute
Dim myXmlAttributeOverrides As New XmlAttributeOverrides()
' Add the XmlAttributes object to the XmlAttributeOverrides object.
myXmlAttributeOverrides.Add(GetType(Student), myXmlAttributes)
' Create the Serializer, and return it.
Dim myXmlSerializer As New XmlSerializer(GetType(Student), myXmlAttributeOverrides)
Return myXmlSerializer
End Function
注釈
ElementName生成された XML 要素の名前をクラス名と異なる名前にする場合は、an を指定します。