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

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

XmlArrayAttribute.Namespace プロパティ

XML 要素名前空間取得または設定します

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

解説解説
使用例使用例

2 つメンバ保持している Library クラスインスタンスシリアル化する例を次に示しますメンバ1 つ書物題名であり、もう 1 つ定期刊行誌題名です。どちらの XML 要素の名前も Titles ですが、プリフィックス異なります。この例には、2 つ要素名を限定するために使用される名前空間プリフィックス格納している、XmlSerializerNamespaces クラスインスタンス含まれています。

Option Explicit
Option Strict

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



Public Class Library
    Private myBooks() As Book
    Private myPeriodicals() As Periodical
    
    ' This element will be qualified with the prefix
    ' that is associated with the namespace http://wwww.cpandl.com.
    
    <XmlArray(ElementName := "Titles", _
        Namespace := "http://wwww.cpandl.com")>
 _ 
    Public Property Books() As
 Book()
        Get
            Return myBooks
        End Get
        Set
            myBooks = value
        End Set
    End Property

    ' This element will be qualified with the prefix that is
    ' associated with the namespace http://www.proseware.com.    
    <XmlArray(ElementName := "Titles", _
        Namespace := "http://www.proseware.com")>
 _
    Public Property Periodicals() As
 Periodical()
        Get
            Return myPeriodicals
        End Get
        Set
            myPeriodicals = value
        End Set
    End Property
End Class
 
Public Class Book
    Public Title As String
    Public Author As String
    Public ISBN As String
    <XmlAttribute()> Public Publisher As
 String
End Class

Public Class Periodical
    Private myTitle As String
    
    Public Property Title() As
 String
        Get
            Return myTitle
        End Get
        Set
            myTitle = value
        End Set
    End Property
End Class
 
Public Class Run
    
    Public Shared Sub Main()
        Dim test As New
 Run()
        test.WriteBook("MyLibrary.xml")
        test.ReadBook("MyLibrary.xml")
    End Sub
    
    
    Public Sub WriteBook(ByVal
 filename As String)
        ' Creates a new XmlSerializer.
        Dim mySerializer As New
 XmlSerializer(GetType(Library))
        ' Writing the file requires a StreamWriter.
        Dim myStreamWriter As New
 StreamWriter(filename)
        ' Creates an XmlSerializerNamespaces and adds prefixes and
        ' namespaces to be used. 
        Dim myNamespaces As New
 XmlSerializerNamespaces()
        myNamespaces.Add("books", "http://wwww.cpandl.com")
        myNamespaces.Add("magazines", "http://www.proseware.com")
        ' Create an instance of the class to be serialized.
        Dim myLibrary As New
 Library()
        
        ' Creates two book objects.
        Dim b1 As New Book()
        b1.Title = "My Book Title"
        b1.Author = "An Author"
        b1.ISBN = "000000000"
        b1.Publisher = "Microsoft Press"
        
        Dim b2 As New Book()
        b2.Title = "Another Book Title"
        b2.Author = "Another Author"
        b2.ISBN = "00000001"
        b2.Publisher = "Another Press"
        
        ' Creates an array using the objects, and sets the Books property
        ' to the array. 
        Dim myBooks As Book() =  {b1, b2}
        myLibrary.Books = myBooks
        
        ' Creates two Periodical objects.
        Dim per1 As New
 Periodical()
        per1.Title = "My Magazine Title"
        Dim per2 As New
 Periodical()
        per2.Title = "Another Magazine Title"
        
        ' Sets the Periodicals property to the array. 
        Dim myPeriodicals() As Periodical =
  {per1, per2}
        myLibrary.Periodicals = myPeriodicals
        
        ' Serializes the myLibrary object.
        mySerializer.Serialize(myStreamWriter, myLibrary, myNamespaces)
        
        myStreamWriter.Close()
    End Sub
    
    
    Public Sub ReadBook(ByVal
 filename As String)
        ' Creates an instance of an XmlSerializer
        ' with the class used to read the document. 
        Dim mySerializer As New
 XmlSerializer(GetType(Library))
        
        ' A FileStream is needed to read the file.
        Dim myFileStream As New
 FileStream(filename, FileMode.Open)
        
        Dim myLibrary As Library = _
            CType(mySerializer.Deserialize(myFileStream), Library)
        
        ' Reads each book in the array returned by the Books property.
      
        Dim i As Integer
        For i = 0 To myLibrary.Books.Length
 - 1
            Console.WriteLine(myLibrary.Books(i).Title)
            Console.WriteLine(myLibrary.Books(i).Author)
            Console.WriteLine(myLibrary.Books(i).ISBN)
            Console.WriteLine(myLibrary.Books(i).Publisher)
            Console.WriteLine()
        Next i
        
        ' Reads each Periodical returned by the Periodicals property.
        For i = 0 To myLibrary.Periodicals.Length
 - 1
            Console.WriteLine(myLibrary.Periodicals(i).Title)
        Next i
    End Sub
End Class

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
 
public class Library
   {
   private Book[] books;
   private Periodical [] periodicals;
   /* This element will be qualified with the prefix 
   that is associated with the namespace http://wwww.cpandl.com.
 */
   [XmlArray(ElementName = "Titles", 
   Namespace="http://wwww.cpandl.com")]
   public Book[] Books
   {
      get{return books;}
      set{books = value;}
   }
   /* This element will be qualified with the prefix that is
   associated with the namespace http://www.proseware.com.
 */
   [XmlArray(ElementName = "Titles", Namespace = 
   "http://www.proseware.com")]
   public Periodical[] Periodicals
   {
      get{return periodicals;}
      set{periodicals = value;}
   }
}
 
public class Book
{
   public string Title;
   public string Author;
   public string ISBN;
   [XmlAttribute]
   public string Publisher;
}
 
public class Periodical
{
   private string title;
   public string Title
   {
      get{return title;}
      set{title = value;}
   }
}
 
public class Run
{
   public static void Main()
   {
      Run test = new Run();
      test.WriteBook("MyLibrary.xml");
      test.ReadBook("MyLibrary.xml");   
   }
 
   public void WriteBook(string
 filename)
   {
      // Creates a new XmlSerializer.
      XmlSerializer mySerializer = new XmlSerializer(typeof(Library));
      // Writing the file requires a StreamWriter.
      TextWriter myStreamWriter = new StreamWriter(filename);
      /* Creates an XmlSerializerNamespaces and adds prefixes and 
      namespaces to be used. */
      XmlSerializerNamespaces myNamespaces = 
      new XmlSerializerNamespaces();
      myNamespaces.Add("books", "http://wwww.cpandl.com");
      myNamespaces.Add("magazines", "http://www.proseware.com");
      // Creates an instance of the class to be serialized.
      Library myLibrary = new Library();
 
      // Creates two book objects.
      Book b1 = new Book();
      b1.Title = "My Book Title";
      b1.Author = "An Author";
      b1.ISBN = "000000000";
      b1.Publisher = "Microsoft Press";
 
      Book b2 = new Book();
      b2.Title = "Another Book Title";
      b2.Author = "Another Author";
      b2.ISBN = "00000001";
      b2.Publisher = "Another Press";
       
      /* Creates an array using the objects, and sets the Books
 property
      to the array. */
      Book[] myBooks = {b1,b2};
      myLibrary.Books = myBooks;
 
      // Creates two Periodical objects.
      Periodical per1 = new Periodical();
      per1.Title = "My Magazine Title";
      Periodical per2 = new Periodical();
      per2.Title = "Another Magazine Title";
 
      // Sets the Periodicals property to the array. 
      Periodical[] myPeridocials = {per1, per2};
      myLibrary.Periodicals = myPeridocials;

      // Serializes the myLibrary object.
      mySerializer.Serialize(myStreamWriter, myLibrary, myNamespaces);

       myStreamWriter.Close();
    }
 
   public void ReadBook(string
 filename)
   {
      /* Creates an instance of an XmlSerializer
      with the class used to read the document. */
      XmlSerializer mySerializer = new XmlSerializer(typeof(Library));
      // A FileStream is needed to read the file.
      FileStream myFileStream = new FileStream(filename, FileMode.Open);

      Library myLibrary = (Library) mySerializer.Deserialize(myFileStream);

      // Reads each book in the array returned by the Books property.
      
      for(int i = 0; i< myLibrary.Books.Length;i++)
      {
         Console.WriteLine(myLibrary.Books[i].Title);
         Console.WriteLine(myLibrary.Books[i].Author);
         Console.WriteLine(myLibrary.Books[i].ISBN);
         Console.WriteLine(myLibrary.Books[i].Publisher);
         Console.WriteLine();
      }
      // Reads each Periodical returned by the Periodicals property.
      for(int i = 0; i< myLibrary.Periodicals.Length;i++)
      {
         Console.WriteLine(myLibrary.Periodicals[i].Title);
      }
   }
}
   
#using <System.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;
public ref class Book
{
public:
   String^ Title;
   String^ Author;
   String^ ISBN;

   [XmlAttributeAttribute]
   String^ Publisher;
};

public ref class Periodical
{
private:
   String^ title;

public:

   property String^ Title 
   {
      String^ get()
      {
         return title;
      }
      void set( String^ value )
      {
         title = value;
      }
   }
};

public ref class Library
{
private:
   array<Book^>^books;
   array<Periodical^>^periodicals;

public:

   /* This element will be qualified with the prefix 
      that is associated with the namespace http://wwww.cpandl.com.
 */
   [XmlArray(ElementName="Titles",
   Namespace="http://wwww.cpandl.com")]
   property array<Book^>^ Books 
   {
      array<Book^>^ get()
      {
         return books;
      }
      void set( array<Book^>^value )
      {
         books = value;
      }
   }

   /* This element will be qualified with the prefix that is
      associated with the namespace http://www.proseware.com.
 */
   [XmlArray(ElementName="Titles",Namespace=
   "http://www.proseware.com")]
   property array<Periodical^>^ Periodicals 
   {
      array<Periodical^>^ get()
      {
         return periodicals;
      }
      void set( array<Periodical^>^value
 )
      {
         periodicals = value;
      }
   }
};

void WriteBook( String^ filename )
{
   // Creates a new XmlSerializer.
   XmlSerializer^ mySerializer = gcnew XmlSerializer( Library::typeid );

   // Writing the file requires a StreamWriter.
   TextWriter^ myStreamWriter = gcnew StreamWriter( filename );

   /* Creates an XmlSerializerNamespaces and adds prefixes and 
      namespaces to be used. */
   XmlSerializerNamespaces^ myNamespaces = gcnew XmlSerializerNamespaces;
   myNamespaces->Add( "books", "http://wwww.cpandl.com"
 );
   myNamespaces->Add( "magazines", "http://www.proseware.com"
 );

   // Creates an instance of the class to be serialized.
   Library^ myLibrary = gcnew Library;

   // Creates two book objects.
   Book^ b1 = gcnew Book;
   b1->Title = "My Book Title";
   b1->Author = "An Author";
   b1->ISBN = "000000000";
   b1->Publisher = "Microsoft Press";
   Book^ b2 = gcnew Book;
   b2->Title = "Another Book Title";
   b2->Author = "Another Author";
   b2->ISBN = "00000001";
   b2->Publisher = "Another Press";

   /* Creates an array using the objects, and sets the Books property
      to the array. */
   array<Book^>^myBooks = {b1,b2};
   myLibrary->Books = myBooks;

   // Creates two Periodical objects.
   Periodical^ per1 = gcnew Periodical;
   per1->Title = "My Magazine Title";
   Periodical^ per2 = gcnew Periodical;
   per2->Title = "Another Magazine Title";

   // Sets the Periodicals property to the array. 
   array<Periodical^>^myPeridocials = {per1,per2};
   myLibrary->Periodicals = myPeridocials;

   // Serializes the myLibrary object.
   mySerializer->Serialize( myStreamWriter, myLibrary, myNamespaces );
   myStreamWriter->Close();
}

void ReadBook( String^ filename )
{
   /* Creates an instance of an XmlSerializer
      with the class used to read the document. */
   XmlSerializer^ mySerializer = gcnew XmlSerializer( Library::typeid );

   // A FileStream is needed to read the file.
   FileStream^ myFileStream = gcnew FileStream( filename,FileMode::Open );
   Library^ myLibrary = dynamic_cast<Library^>(mySerializer->Deserialize(
 myFileStream ));

   // Reads each book in the array returned by the Books property. 
     
   for ( int i = 0; i < myLibrary->Books->Length;
 i++ )
   {
      Console::WriteLine( myLibrary->Books[ i ]->Title );
      Console::WriteLine( myLibrary->Books[ i ]->Author );
      Console::WriteLine( myLibrary->Books[ i ]->ISBN );
      Console::WriteLine( myLibrary->Books[ i ]->Publisher );
      Console::WriteLine();
   }
   for ( int i = 0; i < myLibrary->Periodicals->Length;
 i++ )
   {
      Console::WriteLine( myLibrary->Periodicals[ i ]->Title );
   }
}

int main()
{
   WriteBook( "MyLibrary.xml" );
   ReadBook( "MyLibrary.xml" );
}
import System.*;
import System.IO.*;
import System.Xml.*;
import System.Xml.Serialization.*;

public class Library
{
    private Book books[];
    private Periodical periodicals[];

    /* This element will be qualified with the prefix 
       that is associated with the namespace http://wwww.cpandl.com.
 */
    /** @attribute XmlArray(ElementName = "Titles", 
        Namespace = "http://wwww.cpandl.com")
     */

    /** @property 
     */
    public Book[] get_Books()
    {
        return books;
    } //get_Books

    /** @property 
     */
    public void set_Books(Book value[])
    {
        books = value;
    } //set_Books 

    /* This element will be qualified with the prefix that is
       associated with the namespace http://www.proseware.com.
 
    */
    /** @attribute XmlArray(ElementName = "Titles", 
        Namespace = "http://www.proseware.com")
     */

    /** @property 
     */
    public Periodical[] get_Periodicals()
    {
        return periodicals;
    } //get_Periodicals

    /** @property 
     */
    public void set_Periodicals(Periodical
 value[])
    {
        periodicals = value;
    } //set_Periodicals
} //Library

public class Book
{
    public String title;
    public String author;
    public String isbn;

    /** @attribute XmlAttribute()
     */
    public String publisher;
} //Book

public class Periodical
{
    private String title;

    /** @property 
     */
    public String get_Title()
    {
        return title;
    } //get_Title

    /** @property 
     */
    public void set_Title(String value)
    {
        title = value;
    } //set_Title
} //Periodical

public class Run
{
    public static void main(String[]
 args)
    {
        Run test = new Run();
        test.WriteBook("MyLibrary.xml");
        test.ReadBook("MyLibrary.xml");
    } //main

    public void WriteBook(String fileName)
    {
        // Creates a new XmlSerializer.
        XmlSerializer mySerializer = new XmlSerializer(Library.class.ToType());

        // Writing the file requires a StreamWriter.
        TextWriter myStreamWriter = new StreamWriter(fileName);

        /* Creates an XmlSerializerNamespaces and adds prefixes and 
           namespaces to be used. 
        */
        XmlSerializerNamespaces myNamespaces = new XmlSerializerNamespaces();
        myNamespaces.Add("books", "http://wwww.cpandl.com");
        myNamespaces.Add("magazines", "http://www.proseware.com");

        // Creates an instance of the class to be serialized.
        Library myLibrary = new Library();

        // Creates two book objects.
        Book b1 = new Book();
        b1.title = "My Book Title";
        b1.author = "An Author";
        b1.isbn = "000000000";
        b1.publisher = "Microsoft Press";

        Book b2 = new Book();
        b2.title = "Another Book Title";
        b2.author = "Another Author";
        b2.isbn = "00000001";
        b2.publisher = "Another Press";

        /* Creates an array using the objects, and sets the Books
 property
           to the array. 
        */
        Book myBooks[] =  { b1, b2 };
        myLibrary.set_Books(myBooks);

        // Creates two Periodical objects.
        Periodical per1 = new Periodical();
        per1.set_Title("My Magazine Title");
        Periodical per2 = new Periodical();
        per2.set_Title("Another Magazine Title");

        // Sets the Periodicals property to the array. 
        Periodical myPeridocials[] = { per1, per2 };
        myLibrary.set_Periodicals(myPeridocials);

        // Serializes the myLibrary object.
        mySerializer.Serialize(myStreamWriter, myLibrary, myNamespaces);
        myStreamWriter.Close();
    } //WriteBook

    public void ReadBook(String fileName)
    {
        /* Creates an instance of an XmlSerializer
           with the class used to read the document. 
        */
        XmlSerializer mySerializer = new XmlSerializer(Library.class.ToType());

        // A FileStream is needed to read the file.
        FileStream myFileStream = new FileStream(fileName, FileMode.Open);
        Library myLibrary = (Library)(mySerializer.Deserialize(myFileStream));

        // Reads each book in the array returned by the Books property.
      
        for (int i = 0; i < myLibrary.get_Books().length;
 i++) {
            Console.WriteLine(((Book)myLibrary.get_Books().get_Item(i)).title);
            Console.WriteLine(((Book)myLibrary.get_Books().get_Item(i)).author);
            Console.WriteLine(((Book)myLibrary.get_Books().get_Item(i)).isbn);
            Console.WriteLine(((Book)myLibrary.get_Books().get_Item(i)).
                publisher);
            Console.WriteLine();
        }

        // Reads each Periodical returned by the Periodicals property.
        for (int i = 0; i < myLibrary.get_Periodicals().length;
 i++) {
            Console.WriteLine(((Periodical)myLibrary.get_Periodicals().
                get_Item(i)).get_Title());
        }
    } //ReadBook
} //Run
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
XmlArrayAttribute クラス
XmlArrayAttribute メンバ
System.Xml.Serialization 名前空間
XmlSerializer
Serialize
XmlArrayAttribute.ElementName プロパティ
XmlArrayAttribute.Form プロパティ


このページでは「.NET Framework クラス ライブラリ リファレンス」からXmlArrayAttribute.Namespace プロパティを検索した結果を表示しています。
Weblioに収録されているすべての辞書からXmlArrayAttribute.Namespace プロパティを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からXmlArrayAttribute.Namespace プロパティ を検索

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

辞書ショートカット

すべての辞書の索引

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

   

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



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

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

©2025 GRAS Group, Inc.RSS