次の方法で共有


XmlArrayAttribute.Form プロパティ

XmlSerializer によって生成された XML 要素名が、限定されているかどうかを示す値を取得または設定します。

Public Property Form As XmlSchemaForm
[C#]
public XmlSchemaForm Form {get; set;}
[C++]
public: __property XmlSchemaForm get_Form();public: __property void set_Form(XmlSchemaForm);
[JScript]
public function get Form() : XmlSchemaForm;public function set Form(XmlSchemaForm);

プロパティ値

XmlSchemaForm 値の 1 つ。既定値は XmlSchemaForm.None です。

解説

Form プロパティは、XML 要素名が限定されているかいないかを判断します。 Form プロパティは、W3C (World Wide Web Consortium) のサイト (www.w3.org) で参照できる 1999 年のドキュメント『Namespaces in XML』に準拠しています。

Namespace プロパティに任意の値が設定されている場合は、 Form プロパティを XmlSchemaForm.Unqualified に設定しようとすると例外がスローされます。

既定の設定 XmlSchemaForm.None は、 XmlSerializer に対し、名前空間が限定されているかどうかを判断するために XML ドキュメントのスキーマを調べるように指示します。スキーマが個別の要素または属性に値を指定しない場合、 XmlSerializerelementFormDefault 値および attributeFormDefault 値を使用して、要素または属性が限定されているかどうかを判断します。スキーマの XML コードを次に示します。

<schema elementFormDefault="qualified" 
attributeFormDefault="unqualified" ... >
   <element name="Name"/>
   <attribute name="Number"/>
</schema>

XmlSerializer がスキーマを読み取ると、Name と Number 両方の Form 値は XmlSchemaForm.None になります。ただし、Name 要素は限定されますが、Number 要素は限定されません。

使用例

[Visual Basic, C#, C++] Enterprises クラスのインスタンスをシリアル化する例を次に示します。2 つの XML 要素のローカル名が同一 (Company) ですが、プリフィックスは異なっています。この例では、 Form プロパティが XmlForm.Qualified に設定され、XML インスタンスで限定名が発生するようにします。

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



Public Class Enterprises
    Private mywineries() As Winery
    Private mycompanies() As VacationCompany
    ' Sets the Form property to qualified, and specifies the Namespace. 
    
    <XmlArray(Form := XmlSchemaForm.Qualified, _
        ElementName := "Company", _
        Namespace := "http://www.cohowinery.com")> _
    Public Property Wineries() As Winery()
        
        Get
            Return mywineries
        End Get
        Set
            mywineries = value
        End Set
    End Property 
    
    <XmlArray(Form := XmlSchemaForm.Qualified, _
        ElementName := "Company", _
        Namespace := "http://www.treyresearch.com")> _ 
    Public Property Companies() As VacationCompany()
        
        Get
            Return mycompanies
        End Get
        Set
            mycompanies = value
        End Set
    End Property
End Class 'Enterprises
 
Public Class Winery
    Public Name As String
End Class 'Winery


Public Class VacationCompany
    Public Name As String
End Class 'VacationCompany


Public Class Run
    
    Public Shared Sub Main()
        Dim test As New Run()
        test.WriteEnterprises("MyEnterprises.xml")
    End Sub 'Main
    
    
    Public Sub WriteEnterprises(filename As String)
        ' Creates an instance of the XmlSerializer class.
        Dim mySerializer As New XmlSerializer(GetType(Enterprises))
        ' Writing a file requires a TextWriter.
        Dim writer As New StreamWriter(filename)
        
        ' Creates an instance of the XmlSerializerNamespaces class.
        Dim ns As New XmlSerializerNamespaces()
        
        ' Adds namespaces and prefixes for the XML document instance.
        ns.Add("winery", "http://www.cohowinery.com")
        ns.Add("vacationCompany", "http://www.treyresearch.com")
        
        ' Creates an instance of the class that will be serialized.
        Dim myEnterprises As New Enterprises()
        
        ' Creates objects and adds to the array. 
        Dim w1 As New Winery()
        w1.Name = "cohowinery"
        Dim myWinery(0) As Winery
        myWinery(0) = w1
        myEnterprises.Wineries = myWinery
        
        Dim com1 As New VacationCompany()
        com1.Name = "adventure-works"
        Dim myCompany(0) As VacationCompany
        myCompany(0) = com1
        myEnterprises.Companies = myCompany
        
        ' Serializes the class, and closes the TextWriter.
        mySerializer.Serialize(writer, myEnterprises, ns)
        writer.Close()
    End Sub 'WriteEnterprises
    
    
    Public Sub ReadEnterprises(filename As String)
        Dim mySerializer As New XmlSerializer(GetType(Enterprises))
        Dim fs As New FileStream(filename, FileMode.Open)
        Dim myEnterprises As Enterprises = CType(mySerializer.Deserialize(fs), Enterprises)
        
        Dim i As Integer
        For i = 0 To myEnterprises.Wineries.Length - 1
            Console.WriteLine(myEnterprises.Wineries(i).Name)
        Next i
        For i = 0 To myEnterprises.Companies.Length - 1
            Console.WriteLine(myEnterprises.Companies(i).Name)
        Next i
    End Sub 'ReadEnterprises
End Class 'Run

[C#] 
using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
 
public class Enterprises
{
   private Winery[] wineries;
   private VacationCompany[] companies;
   // Sets the Form property to qualified, and specifies the namespace. 
   [XmlArray(Form = XmlSchemaForm.Qualified, ElementName="Company", 
   Namespace="http://www.cohowinery.com")]
   public Winery[] Wineries{
      get{return wineries;}
      set{wineries = value;}
   }

   [XmlArray(Form = XmlSchemaForm.Qualified, ElementName = "Company", 
   Namespace = "http://www.treyresearch.com")]
   public VacationCompany [] Companies{
      get{return companies;}
      set{companies = value;}
   }
}
 
public class Winery
{
   public string Name;
}
 
public class VacationCompany{
   public string Name;
}
 
public class Run
{
   public static void Main()
   {
      Run test = new Run();
      test.WriteEnterprises("MyEnterprises.xml");
    }
 
   public void WriteEnterprises(string filename)
   {
      // Creates an instance of the XmlSerializer class.
      XmlSerializer mySerializer = 
      new XmlSerializer(typeof(Enterprises));
      // Writing file requires a TextWriter.
      TextWriter writer = new StreamWriter(filename);

      // Creates an instance of the XmlSerializerNamespaces class.
      XmlSerializerNamespaces ns = new XmlSerializerNamespaces();

      // Adds namespaces and prefixes for the XML document instance.
      ns.Add("winery", "http://www.cohowinery.com");
      ns.Add("vacationCompany", "http://www.treyresearch.com");

      // Creates an instance of the class that will be serialized.
      Enterprises myEnterprises = new Enterprises();
      
      // Creates objects and adds to the array. 
      Winery w1= new Winery();
      w1.Name = "cohowinery";
      Winery[]myWinery = {w1};
      myEnterprises.Wineries = myWinery;
 
      VacationCompany com1 = new VacationCompany();
      com1.Name = "adventure-works";
      VacationCompany[] myCompany = {com1};
      myEnterprises.Companies = myCompany;

      // Serializes the class, and closes the TextWriter.
      mySerializer.Serialize(writer, myEnterprises, ns);
      writer.Close();
   }

   public void ReadEnterprises(string filename)
   {
      XmlSerializer mySerializer = 
      new XmlSerializer(typeof(Enterprises));
      FileStream fs = new FileStream(filename, FileMode.Open);
      Enterprises myEnterprises = (Enterprises) 
      mySerializer.Deserialize(fs);
      
      for(int i = 0; i < myEnterprises.Wineries.Length;i++)
      {
         Console.WriteLine(myEnterprises.Wineries[i].Name);
      }   
      for(int i = 0; i < myEnterprises.Companies.Length;i++)
      {
         Console.WriteLine(myEnterprises.Companies[i].Name);
      }
   }
}

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

public __gc class Winery
{
public:
   String* Name;
};

public __gc class VacationCompany{
public:
   String* Name;
};

public __gc class Enterprises
{
private:
   Winery* wineries[];
   VacationCompany* companies[];
   // Sets the Form property to qualified, and specifies the namespace. 
public:
   [XmlArray(Form = XmlSchemaForm::Qualified, ElementName=S"Company", 
      Namespace=S"http://www.cohowinery.com")]
   __property Winery* get_Wineries()[]{
      return wineries;
   }
   __property void set_Wineries( Winery* value[] ){
      wineries = value;
   }

   [XmlArray(Form = XmlSchemaForm::Qualified, ElementName = S"Company", 
      Namespace = S"http://www.treyresearch.com")]
   __property VacationCompany* get_Companies()[]{
      return companies;
   }
   __property void set_Companies( VacationCompany* value[] ){
      companies = value;
   }
};

int main()
{
   String* filename = S"MyEnterprises.xml";
   // Creates an instance of the XmlSerializer class.
   XmlSerializer* mySerializer = 
      new XmlSerializer(__typeof(Enterprises));
   // Writing file requires a TextWriter.
   TextWriter* writer = new StreamWriter(filename);

   // Creates an instance of the XmlSerializerNamespaces class.
   XmlSerializerNamespaces* ns = new XmlSerializerNamespaces();

   // Adds namespaces and prefixes for the XML document instance.
   ns->Add(S"winery", S"http://www.cohowinery.com");
   ns->Add(S"vacationCompany", S"http://www.treyresearch.com");

   // Creates an instance of the class that will be serialized.
   Enterprises* myEnterprises = new Enterprises();

   // Creates objects and adds to the array. 
   Winery* w1= new Winery();
   w1->Name = S"cohowinery";
   Winery* myWinery[] = {w1};
   myEnterprises->Wineries = myWinery;

   VacationCompany* com1 = new VacationCompany();
   com1->Name = S"adventure-works";
   VacationCompany* myCompany[] = {com1};
   myEnterprises->Companies = myCompany;

   // Serializes the class, and closes the TextWriter.
   mySerializer->Serialize(writer, myEnterprises, ns);
   writer->Close();
}

void ReadEnterprises(String* filename)
{
   XmlSerializer* mySerializer = 
      new XmlSerializer(__typeof(Enterprises));
   FileStream* fs = new FileStream(filename, FileMode::Open);
   Enterprises* myEnterprises = dynamic_cast<Enterprises*>(mySerializer->Deserialize(fs));

   for(int i = 0; i < myEnterprises->Wineries->Length;i++)
   {
      Console::WriteLine(myEnterprises->Wineries[i]->Name);
   }   
   for(int i = 0; i < myEnterprises->Companies->Length;i++)
   {
      Console::WriteLine(myEnterprises->Companies[i]->Name);
   }
}

[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 | Namespace