XmlTextAttribute クラスの新しいインスタンスを初期化します。
名前空間: System.Xml.Serialization
アセンブリ: System.Xml (system.xml.dll 内)
構文
'宣言
Public Sub New
'使用
Dim instance As New XmlTextAttribute
public XmlTextAttribute ()
public:
XmlTextAttribute ()
public XmlTextAttribute ()
public function XmlTextAttribute ()
解説
XmlSerializer がパブリック フィールドまたはパブリックな読み書き可能プロパティをシリアル化する方法をオーバーライドするには、XmlAttributes オブジェクトを作成し、その XmlText プロパティに XmlTextAttribute オブジェクトを設定します。詳細については、XmlAttributeOverrides クラスのトピックを参照してください。
使用例
Comment という名前のパブリック フィールドがあるクラスをシリアル化する例を次に示します。この例では XmlTextAttribute をフィールドに適用することで、そのフィールドを XML 要素としてシリアル化する代わりに XML テキストとしてシリアル化します。
Imports System
Imports System.IO
Imports System.Xml.Serialization
Imports System.Xml.Schema
Imports System.Xml
Public Class Group
Public GroupName As String
Public Comment As String
End Class
Public Class Test
Public Shared Sub Main()
Dim t As New Test()
t.SerializerOrder("TextOverride.xml")
End Sub
' Create an instance of the XmlSerializer class that overrides
' the default way it serializes an object.
Public Function CreateOverrider() As XmlSerializer
' CreatE instances of the XmlAttributes and
' XmlAttributeOverrides classes.
Dim attrs As New XmlAttributes()
Dim xOver As New XmlAttributeOverrides()
' Create an XmlTextAttribute to override the default
' serialization process.
Dim xText As New XmlTextAttribute()
attrs.XmlText = xText
' Add the XmlAttributes to the XmlAttributeOverrides.
xOver.Add(GetType(Group), "Comment", attrs)
' Create the XmlSerializer, and return it.
Dim xSer As New XmlSerializer(GetType(Group), xOver)
Return xSer
End Function
Public Sub SerializerOrder(ByVal filename As String)
' Create an XmlSerializer instance.
Dim xSer As XmlSerializer = CreateOverrider()
' Create the object and serialize it.
Dim myGroup As New Group()
myGroup.Comment = "This is a great product."
Dim writer As New StreamWriter(filename)
xSer.Serialize(writer, myGroup)
End Sub
End Class
using System;
using System.IO;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Xml;
public class Group {
public string GroupName;
public string Comment;
}
public class Test {
public static void Main() {
Test t = new Test();
t.SerializerOrder("TextOverride.xml");
}
/* Create an instance of the XmlSerializer class that overrides
the default way it serializes an object. */
public XmlSerializer CreateOverrider() {
/* Create instances of the XmlAttributes and
XmlAttributeOverrides classes. */
XmlAttributes attrs = new XmlAttributes();
XmlAttributeOverrides xOver = new XmlAttributeOverrides();
/* Create an XmlTextAttribute to override the default
serialization process. */
XmlTextAttribute xText = new XmlTextAttribute();
attrs.XmlText = xText;
// Add the XmlAttributes to the XmlAttributeOverrides.
xOver.Add(typeof(Group), "Comment", attrs);
// Create the XmlSerializer, and return it.
XmlSerializer xSer = new XmlSerializer(typeof(Group), xOver);
return xSer;
}
public void SerializerOrder(string filename) {
// Create an XmlSerializer instance.
XmlSerializer xSer = CreateOverrider();
// Create the object and serialize it.
Group myGroup = new Group();
myGroup.Comment = "This is a great product.";
TextWriter writer = new StreamWriter(filename);
xSer.Serialize(writer, myGroup);
}
}
#using <System.Xml.dll>
#using <System.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;
using namespace System::Xml::Schema;
public ref class Group
{
public:
String^ GroupName;
String^ Comment;
};
public ref class Test
{
public:
static void main()
{
Test^ t = gcnew Test;
t->SerializerOrder( "TextOverride.xml" );
}
/* Create an instance of the XmlSerializer class that overrides
the default way it serializes an object. */
XmlSerializer^ CreateOverrider()
{
/* Create instances of the XmlAttributes and
XmlAttributeOverrides classes. */
XmlAttributes^ attrs = gcnew XmlAttributes;
XmlAttributeOverrides^ xOver = gcnew XmlAttributeOverrides;
/* Create an XmlTextAttribute to override the default
serialization process. */
XmlTextAttribute^ xText = gcnew XmlTextAttribute;
attrs->XmlText = xText;
// Add the XmlAttributes to the XmlAttributeOverrides.
xOver->Add( Group::typeid, "Comment", attrs );
// Create the XmlSerializer, and return it.
XmlSerializer^ xSer = gcnew XmlSerializer( Group::typeid,xOver );
return xSer;
}
void SerializerOrder( String^ filename )
{
// Create an XmlSerializer instance.
XmlSerializer^ xSer = CreateOverrider();
// Create the object and serialize it.
Group^ myGroup = gcnew Group;
myGroup->Comment = "This is a great product.";
TextWriter^ writer = gcnew StreamWriter( filename );
xSer->Serialize( writer, myGroup );
}
};
int main()
{
Test::main();
}
import System.*;
import System.IO.*;
import System.Xml.Serialization.*;
import System.Xml.Schema.*;
import System.Xml.*;
public class Group
{
public String groupName;
public String comment;
} //Group
public class Test
{
public static void main(String[] args)
{
Test t = new Test();
t.SerializerOrder("TextOverride.xml");
} //main
/* Create an instance of the XmlSerializer class that overrides
the default way it serializes an object. */
public XmlSerializer CreateOverrider()
{
/* Create instances of the XmlAttributes and
XmlAttributeOverrides classes. */
XmlAttributes attrs = new XmlAttributes();
XmlAttributeOverrides xOver = new XmlAttributeOverrides();
/* Create an XmlTextAttribute to override the default
serialization process. */
XmlTextAttribute xText = new XmlTextAttribute();
attrs.set_XmlText(xText);
// Add the XmlAttributes to the XmlAttributeOverrides.
xOver.Add(Group.class.ToType(), "comment", attrs);
// Create the XmlSerializer, and return it.
XmlSerializer xSer = new XmlSerializer(Group.class.ToType(), xOver);
return xSer;
} //CreateOverrider
public void SerializerOrder(String filename)
{
// Create an XmlSerializer instance.
XmlSerializer xSer = CreateOverrider();
// Create the object and serialize it.
Group myGroup = new Group();
myGroup.comment = "This is a great product.";
TextWriter writer = new StreamWriter(filename);
xSer.Serialize(writer, myGroup);
} //SerializerOrder
} //Test
プラットフォーム
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
バージョン情報
.NET Framework
サポート対象 : 2.0、1.1、1.0
.NET Compact Framework
サポート対象 : 2.0、1.0
参照
関連項目
XmlTextAttribute クラス
XmlTextAttribute メンバ
System.Xml.Serialization 名前空間