Export (0) Print
Expand All

XmlSchemaObject.Namespaces Property

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

Namespace:  System.Xml.Schema
Assembly:  System.Xml (in System.Xml.dll)

public XmlSerializerNamespaces Namespaces { get; set; }

Property Value

Type: System.Xml.Serialization.XmlSerializerNamespaces
The XmlSerializerNamespaces property for the schema object.

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.

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

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;
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";
        s.Includes.Add(import);

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

        s.Items.Add(elem);
        s.Write(Console.Out);

    }
}
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::Xml;
using namespace System::Xml::Schema;

int main() 
{
   XmlSchema* s = new XmlSchema();
   s -> TargetNamespace = S"myNamespace";
   s -> Namespaces->Add(S"myImpPrefix", S"myImportNamespace");

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

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

   s -> Items->Add(elem);
   s -> Write(Console::Out);

}

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" />
</schema>

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft