XmlSchemaObject.Namespaces Property


Gets or sets the XmlSerializerNamespaces to use with this schema object.

 property System::Xml::Serialization::XmlSerializerNamespaces ^ Namespaces { System::Xml::Serialization::XmlSerializerNamespaces ^ get(); void set(System::Xml::Serialization::XmlSerializerNamespaces ^ value); };
public System.Xml.Serialization.XmlSerializerNamespaces Namespaces { get; set; }
member this.Namespaces : System.Xml.Serialization.XmlSerializerNamespaces with get, set
Public Property Namespaces As XmlSerializerNamespaces

Property Value

The XmlSerializerNamespaces property for the schema object.


In the following example, the prefix myImpPrefix is added at the schema element level. The prefix is then used to qualify definitions that are imported from myImportNamespace.

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

using namespace System;
using namespace System::Xml;
using namespace System::Xml::Schema;

int main()
   XmlSchema^ s = gcnew XmlSchema;
   s->TargetNamespace = "myNamespace";
   s->Namespaces->Add( "myImpPrefix", "myImportNamespace" );

   // Create the <xs:import> element.
   XmlSchemaImport^ import = gcnew XmlSchemaImport;
   import->Namespace = "myImportNamespace";
   import->SchemaLocation = "http://www.example.com/myImportNamespace";
   s->Includes->Add( import );

   // Create an element and assign a type from imported schema.
   XmlSchemaElement^ elem = gcnew XmlSchemaElement;
   elem->SchemaTypeName = gcnew XmlQualifiedName( "importType","myImportNamespace" );
   elem->Name = "element1";
   s->Items->Add( elem );
   s->Write( Console::Out );
using System;
using System.Xml;
using System.Xml.Schema;

class XmlSchemaObject
    public static void Main()
        XmlSchema s = new XmlSchema();
        s.TargetNamespace = "myNamespace";
        s.Namespaces.Add("myImpPrefix", "myImportNamespace");

        // Create the <xs:import> element.
        XmlSchemaImport import = new XmlSchemaImport();
        import.Namespace = "myImportNamespace";
        import.SchemaLocation = "http://www.example.com/myImportNamespace";

        // Create an element and assign a type from imported schema.
        XmlSchemaElement elem = new XmlSchemaElement();
        elem.SchemaTypeName = new XmlQualifiedName("importType", "myImportNamespace");
        elem.Name = "element1";

Imports System.Xml
Imports System.Xml.Schema

Class XmlSchemaObject
   Public Shared Sub Main()
      Dim s As New XmlSchema()
      s.TargetNamespace = "myNamespace"
      s.Namespaces.Add("myImpPrefix", "myImportNamespace")
      ' Create the <xs:import> element.
      Dim import As New XmlSchemaImport()
      import.Namespace = "myImportNamespace"
      import.SchemaLocation = "http://www.example.com/myImportNamespace"
      ' Create an element and assign a type from imported schema.
      Dim elem As New XmlSchemaElement()
      elem.SchemaTypeName = New XmlQualifiedName("importType", "myImportNamespace")
      elem.Name = "element1"
   End Sub
End Class

The example produces the following XML.

<?xml version="1.0" encoding="IBM437"?>
<schema xmlns:myImpPrefix="myImportNamespace" targetNamespace="myNamespace" xmlns="http://www.w3.org/2001/XMLSchema">
  <import schemaLocation="http://www.microsoft.com/myImportNamespace" namespace="myImportNamespace" />
  <element name="element1" type="myImpPrefix:importType" />


This gives the Schema Object Model (SOM) the ability to add xmlns declarations. This is useful when you want to declare a prefix to qualify declarations from an imported schema or use in the xpath attribute of the xs:selector element.

You can also use the Namespaces property to change namespace prefixes in a schema. For example, you can change the prefix used by a schema for the W3C XML Schema namespace from xs to xsd as illustrated in the following example.

Dim namespaces As XmlSerializerNamespaces = New XmlSerializerNamespaces()  
namespaces.Add("myChangedPrefix", "myImportNamespace");  
s.Namespaces = namespaces;  
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();  
namespaces.Add("myChangedPrefix", "myImportNamespace");  
s.Namespaces = namespaces;  

Applies to