XmlRootAttribute.ElementName プロパティ
XmlSerializer クラスの Serialize メソッドおよび Deserialize メソッドによって生成および認識される XML 要素名を取得または設定します。
Public Property ElementName As String
[C#]
public string ElementName {get; set;}
[C++]
public: __property String* get_ElementName();public: __property void set_ElementName(String*);
[JScript]
public function get ElementName() : String;public function set ElementName(String);
プロパティ値
XML ドキュメント インスタンスで生成および認識された XML ルート要素名。既定値は、シリアル化されたクラスの名前です。
解説
生成された XML 要素の名前をクラス名とは異なる名前にする場合は、 ElementName を指定します。
使用例
[Visual Basic, C#, C++] XmlRootAttribute クラスのインスタンスを作成し、 ElementName プロパティに新しい値を設定する例を次に示します。次に、このオブジェクトを使用して、オブジェクトのシリアル化をオーバーライドするために使用される XmlAttributeOverrides オブジェクトを作成します。
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 = "https://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
[C#]
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 = "https://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;
}
[C++]
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 = S"Student class1";
// Serialize the class, and close the TextWriter.
TextWriter* writer = new 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 = new XmlAttributes();
// Create an XmlRootAttribute and set its element name and namespace.
XmlRootAttribute* myXmlRootAttribute = new XmlRootAttribute();
myXmlRootAttribute -> ElementName = S"OverriddenRootElementName";
myXmlRootAttribute -> Namespace = S"https://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;
}
[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
参照
XmlRootAttribute クラス | XmlRootAttribute メンバ | System.Xml.Serialization 名前空間