SoapEnumAttribute.Name プロパティ
XmlSerializer が列挙体をシリアル化する場合は 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);
プロパティ値
XmlSerializer が列挙体をシリアル化する場合は XML ドキュメントに生成された値、列挙体メンバを逆シリアル化する場合は認識した値。
解説
生成された XML 列挙子を、元の列挙体の列挙子と区別する場合には、 Name を指定します。
メモ コードでは、 SoapEnumAttribute の代わりに SoapEnum という短い語を使用できます。
使用例
[Visual Basic, C#, C++] XmlSerializer を使用して、 FoodType
という名前の列挙体を含んでいる Food
という名前のクラスをシリアル化する例を次に示します。各列挙体の SoapEnumAttribute を作成し、該当する SoapEnumAttribute を SoapAttributes の SoapEnum プロパティに設定することにより、 FoodType
列挙体をオーバーライドしています。この SoapAttributes が、 XmlSerializer を作成するために使用される SoapAttributeOverrides に追加されます。
Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization
Public Class Group
Public GroupName As String
Public Grouptype As GroupType
End Class
Public enum GroupType
' Use the SoapEnumAttribute to instruct the XmlSerializer
' to generate Small and Large instead of A and B.
<SoapEnum("Small")> _
A
<SoapEnum("Large")> _
B
End enum
Public Class Run
Public Shared Sub Main()
Dim test As Run = new Run()
test.SerializeObject("SoapEnum.xml")
test.SerializeOverride("SoapOverride.xml")
Console.WriteLine("Fininished writing two files")
End Sub
Private Shared Sub SerializeObject(filename As string)
' Create an instance of the XmlSerializer Class.
Dim mapp As XmlTypeMapping = _
(New SoapReflectionImporter()).ImportTypeMapping(GetType(Group))
Dim mySerializer As XmlSerializer = New XmlSerializer(mapp)
' Writing the file requires a TextWriter.
Dim writer As TextWriter = New StreamWriter(filename)
' Create an instance of the Class that will be serialized.
Dim myGroup As Group = New Group()
' Set the object properties.
myGroup.GroupName = ".NET"
myGroup.Grouptype= GroupType.A
' Serialize the Class, and close the TextWriter.
mySerializer.Serialize(writer, myGroup)
writer.Close()
End Sub
Private Sub SerializeOverride(fileName As String)
Dim soapOver As SoapAttributeOverrides = new SoapAttributeOverrides()
Dim SoapAtts As SoapAttributes = new SoapAttributes()
' Add a SoapEnumAttribute for the GroupType.A enumerator. Instead
' of 'A' it will be "West".
Dim soapEnum As SoapEnumAttribute = new SoapEnumAttribute("West")
' Override the "A" enumerator.
SoapAtts.SoapEnum = soapEnum
soapOver.Add(GetType(GroupType), "A", SoapAtts)
' Add another SoapEnumAttribute for the GroupType.B enumerator.
' Instead of 'B' it will be "East".
SoapAtts= New SoapAttributes()
soapEnum = new SoapEnumAttribute()
soapEnum.Name = "East"
SoapAtts.SoapEnum = soapEnum
soapOver.Add(GetType(GroupType), "B", SoapAtts)
' Create an XmlSerializer used for overriding.
Dim map As XmlTypeMapping = New SoapReflectionImporter _
(soapOver).ImportTypeMapping(GetType(Group))
Dim ser As XmlSerializer = New XmlSerializer(map)
Dim myGroup As Group = New Group()
myGroup.GroupName = ".NET"
myGroup.Grouptype = GroupType.B
' Writing the file requires a TextWriter.
Dim writer As TextWriter = New StreamWriter(fileName)
ser.Serialize(writer, myGroup)
writer.Close
End Sub
End Class
[C#]
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
public class Group{
public string GroupName;
public GroupType Grouptype;
}
public enum GroupType{
// Use the SoapEnumAttribute to instruct the XmlSerializer
// to generate Small and Large instead of A and B.
[SoapEnum("Small")]
A,
[SoapEnum("Large")]
B
}
public class Run {
static void Main(){
Run test= new Run();
test.SerializeObject("SoapEnum.xml");
test.SerializeOverride("SoapOverride.xml");
Console.WriteLine("Fininished writing two files");
}
private void SerializeObject(string filename){
// Create an instance of the XmlSerializer Class.
XmlTypeMapping mapp =
(new SoapReflectionImporter()).ImportTypeMapping(typeof(Group));
XmlSerializer mySerializer = new XmlSerializer(mapp);
// Writing the file requires a TextWriter.
TextWriter writer = new StreamWriter(filename);
// Create an instance of the Class that will be serialized.
Group myGroup = new Group();
// Set the object properties.
myGroup.GroupName = ".NET";
myGroup.Grouptype= GroupType.A;
// Serialize the Class, and close the TextWriter.
mySerializer.Serialize(writer, myGroup);
writer.Close();
}
private void SerializeOverride(string fileName){
SoapAttributeOverrides soapOver = new SoapAttributeOverrides();
SoapAttributes SoapAtts = new SoapAttributes();
// Add a SoapEnumAttribute for the GroupType.A enumerator.
// Instead of 'A' it will be "West".
SoapEnumAttribute soapEnum = new SoapEnumAttribute("West");
// Override the "A" enumerator.
SoapAtts.SoapEnum = soapEnum;
soapOver.Add(typeof(GroupType), "A", SoapAtts);
// Add another SoapEnumAttribute for the GroupType.B enumerator.
// Instead of //B// it will be "East".
SoapAtts= new SoapAttributes();
soapEnum = new SoapEnumAttribute();
soapEnum.Name = "East";
SoapAtts.SoapEnum = soapEnum;
soapOver.Add(typeof(GroupType), "B", SoapAtts);
// Create an XmlSerializer used for overriding.
XmlTypeMapping map =
new SoapReflectionImporter(soapOver).
ImportTypeMapping(typeof(Group));
XmlSerializer ser = new XmlSerializer(map);
Group myGroup = new Group();
myGroup.GroupName = ".NET";
myGroup.Grouptype = GroupType.B;
// Writing the file requires a TextWriter.
TextWriter writer = new StreamWriter(fileName);
ser.Serialize(writer, myGroup);
writer.Close();
}
}
[C++]
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;
__value public enum GroupType
{
// Use the SoapEnumAttribute to instruct the XmlSerializer
// to generate Small and Large instead of A and B.
[SoapEnum(S"Small")]
A,
[SoapEnum(S"Large")]
B
};
__gc public class Group
{
public:
String* GroupName;
GroupType Grouptype;
};
__gc public class Run
{
public:
void SerializeObject(String* filename)
{
// Create an instance of the XmlSerializer Class.
XmlTypeMapping * mapp = (new SoapReflectionImporter()) -> ImportTypeMapping(__typeof(Group));
XmlSerializer* mySerializer = new XmlSerializer(mapp);
// Writing the file requires a TextWriter.
TextWriter* writer = new StreamWriter(filename);
// Create an instance of the Class that will be serialized.
Group* myGroup = new Group();
// Set the Object* properties.
myGroup -> GroupName = S".NET";
myGroup -> Grouptype= GroupType::A;
// Serialize the Class, and close the TextWriter.
mySerializer -> Serialize(writer, myGroup);
writer -> Close();
}
void SerializeOverride(String* fileName)
{
SoapAttributeOverrides* soapOver = new SoapAttributeOverrides();
SoapAttributes* SoapAtts = new SoapAttributes();
// Add a SoapEnumAttribute for the GroupType::A enumerator.
// Instead of 'A' it will be S"West".
SoapEnumAttribute* soapEnum = new SoapEnumAttribute(S"West");
// Override the S"A" enumerator.
SoapAtts -> SoapEnum = soapEnum;
soapOver -> Add(__typeof(GroupType), S"A", SoapAtts);
// Add another SoapEnumAttribute for the GroupType::B enumerator.
// Instead of //B// it will be S"East".
SoapAtts = new SoapAttributes();
soapEnum = new SoapEnumAttribute();
soapEnum -> Name = S"East";
SoapAtts -> SoapEnum = soapEnum;
soapOver -> Add(__typeof(GroupType), S"B", SoapAtts);
// Create an XmlSerializer used for overriding.
XmlTypeMapping* map = (new SoapReflectionImporter(soapOver))->ImportTypeMapping(__typeof(Group));
XmlSerializer* ser = new XmlSerializer(map);
Group* myGroup = new Group();
myGroup -> GroupName = S".NET";
myGroup -> Grouptype = GroupType::B;
// Writing the file requires a TextWriter.
TextWriter* writer = new StreamWriter(fileName);
ser -> Serialize(writer, myGroup);
writer -> Close();
}
};
int main()
{
Run* test = new Run();
test -> SerializeObject(S"SoapEnum.xml");
test -> SerializeOverride(S"SoapOverride.xml");
Console::WriteLine(S"Fininished writing two files");
}
[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
参照
SoapEnumAttribute クラス | SoapEnumAttribute メンバ | System.Xml.Serialization 名前空間