XmlSchemaObject.Namespaces 屬性

定義

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

C#
public System.Xml.Serialization.XmlSerializerNamespaces Namespaces { get; set; }

屬性值

XmlSerializerNamespaces

結構描述物件的 XmlSerializerNamespaces 屬性。

範例

在下列範例中,myImpPrefix 前置詞會在架構元素層級新增。 接著會使用前置詞來限定從 myImportNamespace 匯入的定義。

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

此範例會產生下列 XML。

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,如下列範例所示。

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

適用於

產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 2.0, 2.1