Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

XmlArrayAttribute.Form Property

Gets or sets a value that indicates whether the XML element name generated by the XmlSerializer is qualified or unqualified.

Namespace: System.Xml.Serialization
Assembly: System.Xml (in system.xml.dll)

public XmlSchemaForm Form { get; set; }
/** @property */
public XmlSchemaForm get_Form ()

/** @property */
public void set_Form (XmlSchemaForm value)

public function get Form () : XmlSchemaForm

public function set Form (value : XmlSchemaForm)

Property Value

One of the XmlSchemaForm values. The default is XmlSchemaForm.None.

The Form property determines whether an XML element name is qualified or unqualified. The Form property conforms to the 1999 World Wide Web Consortium (www.w3.org) document titled "Namespaces in XML."

If the Namespace property is set to any value, attempting to set the Form property to XmlSchemaForm.Unqualified throws an exception.

The default setting, XmlSchemaForm.None, instructs the XmlSerializer to check the schema for the XML document to determine whether the namespace is qualified. If the schema does not specify a value for an individual element or attribute, the XmlSerializer uses the elementFormDefault and attributeFormDefault values to determine whether an element or attribute is qualified. The following XML code shows a schema:

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

When the XmlSerializer reads the schema, the Form value for both the Name and Number is XmlSchemaForm.None, but the Name element is qualified, while the Number element is unqualified.

The following example serializes an instance of the Enterprises class. Two XML elements have the same local name (Company) but different prefixes. The example sets the Form property is set to XmlForm.Qualified to ensure that the qualified names occur in the XML instance.

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);
      }
   }
}

import System.*;
import System.IO.*;
import System.Xml.*;
import System.Xml.Schema.*;
import System.Xml.Serialization.*;

public class Enterprises
{
    private Winery wineries[];
    private VacationCompany companies[];

    // Sets the Form property to qualified, and specifies the namespace. 
    /** @attribute XmlArray(Form = XmlSchemaForm.Qualified, 
        ElementName = "Company", Namespace = "http://www.cohowinery.com")
     */
    /** @property 
     */
    public Winery[] get_Wineries()
    {
        return wineries;
    } //get_Wineries

    /** @property 
     */
    public void set_Wineries(Winery value[])
    {
        wineries = value;
    } //set_Wineries

    /** @attribute XmlArray(Form = XmlSchemaForm.Qualified, 
        ElementName = "Company", Namespace = "http://www.treyresearch.com")
     */
    /** @property 
     */
    public VacationCompany[] get_Companies()
    {
        return companies;
    } //get_Companies

    /** @property 
     */
    public void set_Companies(VacationCompany value[])
    {
        companies = value;
    } //set_Companies
} //Enterprises

public class Winery
{
    public String name;
} //Winery

public class VacationCompany
{
    public String name;
} //VacationCompany

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

    public void WriteEnterprises(String fileName)
    {
        // Creates an instance of the XmlSerializer class.
        XmlSerializer mySerializer = 
            new XmlSerializer(Enterprises.class.ToType());

        // 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.set_Wineries(myWinery);

        VacationCompany com1 = new VacationCompany();
        com1.name = "adventure-works";
        VacationCompany myCompany[] = { com1 };
        myEnterprises.set_Companies(myCompany);

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

    public void ReadEnterprises(String fileName)
    {
        XmlSerializer mySerializer = 
            new XmlSerializer(Enterprises.class.ToType());
        FileStream fs = new FileStream(fileName, FileMode.Open);
        Enterprises myEnterprises = 
            (Enterprises)(mySerializer.Deserialize(fs));

        for (int i = 0; i < myEnterprises.get_Wineries().length; i++) {
            Console.WriteLine(((Winery)myEnterprises.get_Wineries().
                get_Item(i)).name);
        }

        for (int i = 0; i < myEnterprises.get_Companies().length; i++) {
            Console.WriteLine(((VacationCompany)myEnterprises.get_Companies().
                get_Item(i)).name);
        }
    } //ReadEnterprises
} //Run

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2015 Microsoft