次の方法で共有


XmlArrayAttribute.Namespace プロパティ

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

Public Property Namespace As String
[C#]
public string Namespace {get; set;}
[C++]
public: __property String* get_Namespace();public: __property void set_Namespace(String*);
[JScript]
public function get Namespace() : String;public function set Namespace(String);

プロパティ値

XML 要素の名前空間。

解説

Namespace プロパティを使用すると、限定された XML 要素名を作成できます。 Namespace プロパティは、W3C (World Wide Web Consortium) のサイト (www.w3.org) で参照できるドキュメント『Namespaces in XML』(1999 年) の XML 名前空間の作成のための規則に準拠しています。

プリフィックスに関連付けられている名前空間を作成するには、XML ドキュメントで使用する名前空間とプリフィックスを含んでいる、 XmlSerializerNamespaces クラスのインスタンスを作成する必要があります。各 XmlArrayAttribute に対して設定する名前空間は、 XmlSerializerNamespaces 内の名前空間のいずれか 1 つと一致する必要があります。XML が生成されると、各配列には、指定した名前空間と関連付けられたプリフィックスが正しく付けられます。

使用例

[Visual Basic, C#, C++] 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 https://www.proseware.com.    
    <XmlArray(ElementName := "Titles", _
        Namespace := "https://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", "https://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


[C#] 
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 https://www.proseware.com. */
   [XmlArray(ElementName = "Titles", Namespace = 
   "https://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", "https://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);
      }
   }
}
   

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;

public __gc class Book
{
public:
   String* Title;
   String* Author;
   String* ISBN;
   [XmlAttributeAttribute]
   String* Publisher;
};

public __gc class Periodical
{
private:
   String* title;
public:
   __property String* get_Title(){return title;}
   __property void set_Title( String* value ){title = value;}

};

public __gc class Library
{
private:
   Book* books[];
   Periodical* periodicals[];
public:
   /* This element will be qualified with the prefix 
   that is associated with the namespace http://wwww.cpandl.com. */
   [XmlArray(ElementName = S"Titles", 
      Namespace=S"http://wwww.cpandl.com")]
   __property Book*get_Books()[]{return books;}
   __property void set_Books( Book* value[] ){books = value;}

   /* This element will be qualified with the prefix that is
   associated with the namespace https://www.proseware.com. */
   [XmlArray(ElementName = S"Titles", Namespace = 
      S"https://www.proseware.com")]
   __property Periodical*get_Periodicals()[]{return periodicals;}
   __property void set_Periodicals( Periodical* value[] ){periodicals = value;}

};

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(S"books", S"http://wwww.cpandl.com");
   myNamespaces->Add(S"magazines", S"https://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 = S"My Book Title";
   b1->Author = S"An Author";
   b1->ISBN = S"000000000";
   b1->Publisher = S"Microsoft Press";

   Book* b2 = new Book();
   b2->Title = S"Another Book Title";
   b2->Author = S"Another Author";
   b2->ISBN = S"00000001";
   b2->Publisher = S"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 = S"My Magazine Title";
   Periodical* per2 = new Periodical();
   per2->Title = S"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();
}

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 = 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();
   }
   // Reads each Periodical returned by the Periodicals property.
   for(int i = 0; i< myLibrary->Periodicals->Length;i++)
   {
      Console::WriteLine(myLibrary->Periodicals[i]->Title);
   }
}

int main()
{
   WriteBook(S"MyLibrary.xml");
   ReadBook(S"MyLibrary.xml");   
}

[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

参照

XmlArrayAttribute クラス | XmlArrayAttribute メンバ | System.Xml.Serialization 名前空間 | XmlSerializer | Serialize | ElementName | Form