XmlAttributes.XmlType プロパティとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > XmlAttributes.XmlType プロパティの意味・解説 

XmlAttributes.XmlType プロパティ

XmlTypeAttribute が適用されているクラスを XmlSerializer がシリアル化する方法指定するオブジェクト取得または指定します

名前空間: System.Xml.Serialization
アセンブリ: System.Xml (system.xml.dll 内)
構文構文

Public Property XmlType As
 XmlTypeAttribute
Dim instance As XmlAttributes
Dim value As XmlTypeAttribute

value = instance.XmlType

instance.XmlType = value
public XmlTypeAttribute XmlType { get; set;
 }
public:
property XmlTypeAttribute^ XmlType {
    XmlTypeAttribute^ get ();
    void set (XmlTypeAttribute^ value);
}
/** @property */
public XmlTypeAttribute get_XmlType ()

/** @property */
public void set_XmlType (XmlTypeAttribute value)
public function get XmlType
 () : XmlTypeAttribute

public function set XmlType
 (value : XmlTypeAttribute)

プロパティ
クラス宣言適用されXmlTypeAttributeオーバーライドする XmlTypeAttribute

解説解説

XmlTypeAttribute使用してXmlSerializer による型のシリアル化方法制御できます。たとえば、既定では、型がシリアル化されるときは、XmlSerializerクラス名XML 要素名として使用しますXmlTypeAttribute作成し、それに XmlType プロパティ設定し、XmlAttributeOverrides オブジェクト作成することによって、XML 要素名を変更できます

使用例使用例

XmlTypeAttribute オブジェクト作成し、そのオブジェクトを XmlAttributes オブジェクトXmlType プロパティ代入する例を次に示します

Imports System
Imports System.IO
Imports System.Xml.Serialization


Public Class Transportation
    Public Cars() As Car
End Class

Public Class Car
    Public ID As Integer
End Class


Public Class Test
    
    Public Shared Sub Main()
        Dim t As New Test()
        t.SerializeObject("XmlType.xml")
    End Sub
    
    
    ' Return an XmlSerializer used for overriding.
    Public Function CreateOverrider() As
 XmlSerializer
        ' Create the XmlAttributes and XmlAttributeOverrides objects.
        Dim attrs As New
 XmlAttributes()
        Dim xOver As New
 XmlAttributeOverrides()
        
        ' Create an XmlTypeAttribute and change the name of the
        ' XML type. 
        Dim xType As New
 XmlTypeAttribute()
        xType.TypeName = "Autos"
        
        ' Set the XmlTypeAttribute to the XmlType property.
        attrs.XmlType = xType
        
        ' Add the XmlAttributes to the XmlAttributeOverrides,
        ' specifying the member to override. 
        xOver.Add(GetType(Car), attrs)
        
        ' Create the XmlSerializer, and return it.
        Dim xSer As New
 XmlSerializer(GetType(Transportation), xOver)
        Return xSer
    End Function
    
    
    Public Sub SerializeObject(ByVal
 filename As String)
        ' Create an XmlSerializer instance.
        Dim xSer As XmlSerializer = CreateOverrider()
        
        ' Create an object and serialize it.
        Dim myTransportation As New
 Transportation()
        
        Dim c1 As New Car()
        c1.ID = 12
        
        Dim c2 As New Car()
        c2.ID = 44
        
        myTransportation.Cars = New Car(1) {c1, c2}
        
        ' To write the file, a TextWriter is required.
        Dim writer As New
 StreamWriter(filename)
        xSer.Serialize(writer, myTransportation)
    End Sub
End Class

using System;
using System.IO;
using System.Xml.Serialization;

public class Transportation
{
   public Car[] Cars;
}

public class Car
{
   public int ID;
}

public class Test
{
   public static void Main()
   {
      Test t = new Test();
      t.SerializeObject("XmlType.xml");
   }

   // Return an XmlSerializer used for overriding.
   public XmlSerializer CreateOverrider()
   {
      // Create the XmlAttributes and XmlAttributeOverrides objects.
      XmlAttributes attrs = new XmlAttributes();
      XmlAttributeOverrides xOver = new XmlAttributeOverrides();
      
      /* Create an XmlTypeAttribute and change the name of the 
         XML type. */
      XmlTypeAttribute xType = new XmlTypeAttribute();
      xType.TypeName = "Autos";

      // Set the XmlTypeAttribute to the XmlType property.
      attrs.XmlType = xType;

      /* Add the XmlAttributes to the XmlAttributeOverrides,
         specifying the member to override. */
      xOver.Add(typeof(Car), attrs);

      // Create the XmlSerializer, and return it.
      XmlSerializer xSer = new XmlSerializer
      (typeof(Transportation), xOver);
      return xSer;
   }

   public void SerializeObject(string
 filename)
   {
      // Create an XmlSerializer instance.
      XmlSerializer xSer = CreateOverrider();

      // Create object and serialize it.
      Transportation myTransportation = 
      new Transportation();

      Car c1 = new Car();
      c1.ID = 12;

      Car c2 = new Car();
      c2.ID = 44;

      myTransportation.Cars = new Car[2]{c1,c2};

      // To write the file, a TextWriter is required.
      TextWriter writer = new StreamWriter(filename);
      xSer.Serialize(writer, myTransportation);
   }
}

#using <System.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml::Serialization;
public ref class Car
{
public:
   int ID;
};

public ref class Transportation
{
public:
   array<Car^>^Cars;
};

// Return an XmlSerializer used for overriding.
XmlSerializer^ CreateOverrider()
{
   // Create the XmlAttributes and XmlAttributeOverrides objects.
   XmlAttributes^ attrs = gcnew XmlAttributes;
   XmlAttributeOverrides^ xOver = gcnew XmlAttributeOverrides;

   /* Create an XmlTypeAttribute and change the name of the 
      XML type. */
   XmlTypeAttribute^ xType = gcnew XmlTypeAttribute;
   xType->TypeName = "Autos";

   // Set the XmlTypeAttribute to the XmlType property.
   attrs->XmlType = xType;

   /* Add the XmlAttributes to the XmlAttributeOverrides,
      specifying the member to override. */
   xOver->Add( Car::typeid, attrs );

   // Create the XmlSerializer, and return it.
   XmlSerializer^ xSer = gcnew XmlSerializer( Transportation::typeid,xOver );
   return xSer;
}

void SerializeObject( String^ filename )
{
   // Create an XmlSerializer instance.
   XmlSerializer^ xSer = CreateOverrider();

   // Create object and serialize it.
   Transportation^ myTransportation = gcnew Transportation;
   Car^ c1 = gcnew Car;
   c1->ID = 12;
   Car^ c2 = gcnew Car;
   c2->ID = 44;
   array<Car^>^temp0 = {c1,c2};
   myTransportation->Cars = temp0;

   // To write the file, a TextWriter is required.
   TextWriter^ writer = gcnew StreamWriter( filename );
   xSer->Serialize( writer, myTransportation );
}

int main()
{
   SerializeObject( "XmlType.xml" );
}
import System.*;
import System.IO.*;
import System.Xml.Serialization.*;

public class Transportation
{
    public Car cars[];
} //Transportation

public class Car
{
    public int id;
} //Car

public class Test
{
    public static void main(String[]
 args)
    {
        Test t = new Test();
        t.SerializeObject("XmlType.xml");
    } //main

    // Return an XmlSerializer used for overriding.
    public XmlSerializer CreateOverrider()
    {
        // Create the XmlAttributes and XmlAttributeOverrides objects.
        XmlAttributes attrs = new XmlAttributes();
        XmlAttributeOverrides xOver = new XmlAttributeOverrides();

        /* Create an XmlTypeAttribute and change the name of the 
           XML type. */
        XmlTypeAttribute xType = new XmlTypeAttribute();
        xType.set_TypeName("Autos");

        // Set the XmlTypeAttribute to the XmlType property.
        attrs.set_XmlType(xType);

        /* Add the XmlAttributes to the XmlAttributeOverrides,
           specifying the member to override. */
        xOver.Add(Car.class.ToType(), attrs);

        // Create the XmlSerializer, and return it.
        XmlSerializer xSer =
            new XmlSerializer(Transportation.class.ToType(),
 xOver);

        return xSer;
    } //CreateOverrider

    public void SerializeObject(String fileName)
    {
        // Create an XmlSerializer instance.
        XmlSerializer xSer = CreateOverrider();

        // Create object and serialize it.
        Transportation myTransportation = new Transportation();
        Car c1 = new Car();
        c1.id = 12;
        Car c2 = new Car();
        c2.id = 44;
        myTransportation.cars = new Car[] { c1, c2 };

        // To write the file, a TextWriter is required.
        TextWriter writer = new StreamWriter(fileName);
        xSer.Serialize(writer, myTransportation);
    } //SerializeObject
} //Test
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
XmlAttributes クラス
XmlAttributes メンバ
System.Xml.Serialization 名前空間



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

XmlAttributes.XmlType プロパティのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



XmlAttributes.XmlType プロパティのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS