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

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

XmlAttributes.XmlRoot プロパティ

XmlSerializer がクラスXML ルート要素としてシリアル化する方法指定するオブジェクト取得または指定します

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

Public Property XmlRoot As
 XmlRootAttribute
Dim instance As XmlAttributes
Dim value As XmlRootAttribute

value = instance.XmlRoot

instance.XmlRoot = value
public XmlRootAttribute XmlRoot { get; set;
 }
public:
property XmlRootAttribute^ XmlRoot {
    XmlRootAttribute^ get ();
    void set (XmlRootAttribute^ value);
}
/** @property */
public XmlRootAttribute get_XmlRoot ()

/** @property */
public void set_XmlRoot (XmlRootAttribute value)
public function get XmlRoot
 () : XmlRootAttribute

public function set XmlRoot
 (value : XmlRootAttribute)

プロパティ
XML ルート要素として属性設定されているクラスオーバーライドする XmlRootAttribute。

使用例使用例

XmlAttributeOverrides、XmlAttributes、XmlRootAttribute の各オブジェクト作成する例を次に示します。この例では、XmlRootAttributeXmlAttributes オブジェクトXmlRoot プロパティ代入し、XmlAttributes オブジェクトXmlAttributeOverrides オブジェクト追加します最後にこの例では、シリアル化されたクラスTypeXmlAttributeOverrides オブジェクトに渡すことにより XmlAttributes オブジェクト取得します。この例では、TypeGroup です。

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


' This is the class that will be serialized.
Public Class Group
    Public GroupName As String
    <XmlAttribute()> Public GroupCode As
 Integer
End Class


Public Class Test
    
    Public Shared Sub Main()
        Dim t As New Test()
        t.SerializeObject("OverrideRoot.xml")
    End Sub
    
    
    ' Return an XmlSerializer for overriding attributes.
    Public Function CreateOverrider() As
 XmlSerializer
        ' Create the XmlAttributes and XmlAttributeOverrides objects.
        Dim attrs As New
 XmlAttributes()
        Dim xOver As New
 XmlAttributeOverrides()
        
        Dim xRoot As New
 XmlRootAttribute()
        
        ' Set a new Namespace and ElementName for the root element.
        xRoot.Namespace = "http://www.cpandl.com"
        xRoot.ElementName = "NewGroup"
        attrs.XmlRoot = xRoot
        
        ' Add the XmlAttributes object to the XmlAttributeOverrides.
        ' No  member name is needed because the whole class is
        ' overridden. 
        xOver.Add(GetType(Group), attrs)
        
        ' Get the XmlAttributes object, based on the type.
        Dim tempAttrs As XmlAttributes
        tempAttrs = xOver(GetType(Group))
        
        ' Print the Namespace and ElementName of the root.
        Console.WriteLine(tempAttrs.XmlRoot.Namespace)
        Console.WriteLine(tempAttrs.XmlRoot.ElementName)
        
        Dim xSer As New
 XmlSerializer(GetType(Group), xOver)
        Return xSer
    End Function
    
    
    Public Sub SerializeObject(ByVal
 filename As String)
        ' Create the XmlSerializer using the CreateOverrider method.
        Dim xSer As XmlSerializer = CreateOverrider()
        
        ' Create the object to serialize.
        Dim myGroup As New
 Group()
        myGroup.GroupName = ".NET"
        myGroup.GroupCode = 123
        
        ' To write the file, a TextWriter is required.
        Dim writer As New
 StreamWriter(filename)
        
        ' Serialize the object and close the TextWriter.
        xSer.Serialize(writer, myGroup)
        writer.Close()
    End Sub
End Class

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

// This is the class that will be serialized.
public class Group
{
   public string GroupName;
   [XmlAttribute]
   public int GroupCode;
}

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

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

      // Set a new Namespace and ElementName for the root element.
      xRoot.Namespace = "http://www.cpandl.com";
      xRoot.ElementName = "NewGroup";
      attrs.XmlRoot = xRoot;

      /* Add the XmlAttributes object to the XmlAttributeOverrides. 
         No  member name is needed because the whole class is
 
         overridden. */
      xOver.Add(typeof(Group), attrs);

      // Get the XmlAttributes object, based on the type.
      XmlAttributes tempAttrs;
      tempAttrs = xOver[typeof(Group)];

      // Print the Namespace and ElementName of the root.
      Console.WriteLine(tempAttrs.XmlRoot.Namespace);
      Console.WriteLine(tempAttrs.XmlRoot.ElementName);

      XmlSerializer xSer = new XmlSerializer(typeof(Group), xOver);
      return xSer;
   }

   public void SerializeObject(string
 filename)
   {
      // Create the XmlSerializer using the CreateOverrider method.
      XmlSerializer xSer = CreateOverrider();

      // Create the object to serialize.
      Group myGroup = new Group();
      myGroup.GroupName = ".NET";
      myGroup.GroupCode = 123;

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

      // Serialize the object and close the TextWriter.
      xSer.Serialize(writer, myGroup);
      writer.Close();
   }
}

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

using namespace System;
using namespace System::IO;
using namespace System::Xml::Serialization;

// This is the class that will be serialized.
public ref class Group
{
public:
   String^ GroupName;

   [XmlAttributeAttribute]
   int GroupCode;
};

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

   // Set a new Namespace and ElementName for the root element.
   xRoot->Namespace = "http://www.cpandl.com";
   xRoot->ElementName = "NewGroup";
   attrs->XmlRoot = xRoot;

   /* Add the XmlAttributes object to the XmlAttributeOverrides. 
      No  member name is needed because the whole class is 
      overridden. */
   xOver->Add( Group::typeid, attrs );

   // Get the XmlAttributes object, based on the type.
   XmlAttributes^ tempAttrs;
   tempAttrs = xOver[ Group::typeid ];

   // Print the Namespace and ElementName of the root.
   Console::WriteLine( tempAttrs->XmlRoot->Namespace );
   Console::WriteLine( tempAttrs->XmlRoot->ElementName );
   XmlSerializer^ xSer = gcnew XmlSerializer( Group::typeid,xOver );
   return xSer;
}

void SerializeObject( String^ filename )
{
   // Create the XmlSerializer using the CreateOverrider method.
   XmlSerializer^ xSer = CreateOverrider();

   // Create the object to serialize.
   Group^ myGroup = gcnew Group;
   myGroup->GroupName = ".NET";
   myGroup->GroupCode = 123;

   // To write the file, a TextWriter is required.
   TextWriter^ writer = gcnew StreamWriter( filename );

   // Serialize the object and close the TextWriter.
   xSer->Serialize( writer, myGroup );
   writer->Close();
}

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

// This is the class that will be serialized.
public class Group
{
    public String groupName;
    /** @attribute XmlAttribute()
     */
    public int groupCode;
} //Group

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

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

        // Set a new Namespace and ElementName for the root element.
        xRoot.set_Namespace("http://www.cpandl.com");
        xRoot.set_ElementName("NewGroup");
        attrs.set_XmlRoot(xRoot);

        /* Add the XmlAttributes object to the XmlAttributeOverrides. 
           No  member name is needed because the whole class is
 
           overridden. */
        xOver.Add(Group.class.ToType(), attrs);

        // Get the XmlAttributes object, based on the type.
        XmlAttributes tempAttrs;
        tempAttrs = xOver.get_Item(Group.class.ToType());

        // Print the Namespace and ElementName of the root.
        Console.WriteLine(tempAttrs.get_XmlRoot().get_Namespace());
        Console.WriteLine(tempAttrs.get_XmlRoot().get_ElementName());

        XmlSerializer xSer = new XmlSerializer(Group.class.ToType(),
 xOver);
        return xSer;
    } //CreateOverrider

    public void SerializeObject(String fileName)
    {
        // Create the XmlSerializer using the CreateOverrider method.
        XmlSerializer xSer = CreateOverrider();

        // Create the object to serialize.
        Group myGroup = new Group();
        myGroup.groupName = ".NET";
        myGroup.groupCode = 123;

        // To write the file, a TextWriter is required.
        TextWriter writer = new StreamWriter(fileName);

        // Serialize the object and close the TextWriter.
        xSer.Serialize(writer, myGroup);
        writer.Close();
    } //SerializeObject
} //Test
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
XmlAttributes クラス
XmlAttributes メンバ
System.Xml.Serialization 名前空間



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

辞書ショートカット

すべての辞書の索引

「XmlAttributes.XmlRoot プロパティ」の関連用語

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

   

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



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

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

©2024 GRAS Group, Inc.RSS