XmlSchemaObject.Namespaces 屬性

定義

取得或設定 XmlSerializerNamespaces,以便與這個結構描述物件一起使用。

public:
 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

屬性值

XmlSerializerNamespaces

結構描述物件的 XmlSerializerNamespaces 屬性。

範例

在下列範例中,myImpPrefix 前置詞會在架構元素層級新增。 接著會使用前置詞來限定從 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";
        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);
    }
}
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"
      s.Includes.Add(import)
      
      ' Create an element and assign a type from imported schema.
      Dim elem As New XmlSchemaElement()
      elem.SchemaTypeName = New XmlQualifiedName("importType", "myImportNamespace")
      elem.Name = "element1"
      
      s.Items.Add(elem)
      s.Write(Console.Out)
   End Sub
End Class

此範例會產生下列 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>

備註

這可讓架構物件模型 (SOM) 新增 xmlns 宣告的能力。 當您想要宣告前置詞以限定匯入架構的宣告,或在 xs:selector 元素的 xpath 屬性中使用時,這會很有用。

您也可以使用 Namespaces 屬性來變更架構中的命名空間前置詞。 例如,您可以將 W3C XML 架構命名空間架構所使用的前置詞從 xs 變更為 xsd,如下列範例所示。

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

適用於