語言

XmlTextWriter.WriteQualifiedName(String, String) 方法

定義

寫出命名空間限定的名稱。 此方法查找該命名空間範圍內的前綴。

public:
 override void WriteQualifiedName(System::String ^ localName, System::String ^ ns);
public override void WriteQualifiedName(string localName, string? ns);
public override void WriteQualifiedName(string localName, string ns);
override this.WriteQualifiedName : string * string -> unit
Public Overrides Sub WriteQualifiedName (localName As String, ns As String)

參數

localName
String

當地的名稱。

ns
String

命名空間 URI 來關聯到這個名稱。

例外狀況

localName 是 null 或 String.Empty。

localName 根據 W3C 命名空間規範,該名稱並不有效。

範例

以下範例寫出 XSD 結構的一部分。

using System;
using System.IO;
using System.Xml;

public class Sample
{
  private const string filename = "sampledata.xml";

  public static void Main()
  {
     XmlTextWriter writer = null;

     writer = new XmlTextWriter (filename, null);
     // Use indenting for readability.
     writer.Formatting = Formatting.Indented;

     // Write the root element.
     writer.WriteStartElement("schema");

     // Write the namespace declarations.
     writer.WriteAttributeString("xmlns", null,"http://www.w3.org/2001/XMLSchema");
     writer.WriteAttributeString("xmlns","po",null,"http://contoso.com/po");

     writer.WriteStartElement("element");

     writer.WriteAttributeString("name", "purchaseOrder");

     // Write the type attribute.
     writer.WriteStartAttribute(null,"type", null);
     writer.WriteQualifiedName("PurchaseOrder", "http://contoso.com/po");
     writer.WriteEndAttribute();

     writer.WriteEndElement();

     // Write the close tag for the root element.
     writer.WriteEndElement();

     // Write the XML to file and close the writer.
     writer.Flush();
     writer.Close();

     // Read the file back in and parse to ensure well formed XML.
     XmlDocument doc = new XmlDocument();
     // Preserve white space for readability.
     doc.PreserveWhitespace = true;
     // Load the file.
     doc.Load(filename);

     // Write the XML content to the console.
     Console.Write(doc.InnerXml);
  }
}
Option Explicit
Option Strict

Imports System.IO
Imports System.Xml

Public Class Sample
    Private Shared filename As String = "sampledata.xml"
    Public Shared Sub Main()
        Dim writer As XmlTextWriter = Nothing
        
        writer = New XmlTextWriter(filename, Nothing)
        ' Use indenting for readability.
        writer.Formatting = Formatting.Indented
        
        ' Write the root element.
        writer.WriteStartElement("schema")
        
        ' Write the namespace declarations.
        writer.WriteAttributeString("xmlns", Nothing, "http://www.w3.org/2001/XMLSchema")
        writer.WriteAttributeString("xmlns", "po", Nothing, "http://contoso.com/po")
        
        writer.WriteStartElement("element")
        
        writer.WriteAttributeString("name", "purchaseOrder")
        
        ' Write the type attribute.
        writer.WriteStartAttribute(Nothing, "type", Nothing)
        writer.WriteQualifiedName("PurchaseOrder", "http://contoso.com/po")
        writer.WriteEndAttribute()
        
        writer.WriteEndElement()
        
        ' Write the close tag for the root element.
        writer.WriteEndElement()
        
        ' Write the XML to file and close the writer.
        writer.Flush()
        writer.Close()
        
        ' Read the file back in and parse to ensure well formed XML.
        Dim doc As New XmlDocument()
        ' Preserve white space for readability.
        doc.PreserveWhitespace = True
        ' Load the file.
        doc.Load(filename)
        
        ' Write the XML content to the console.
        Console.Write(doc.InnerXml)
    End Sub
End Class

備註

Note

建議您使用 XmlWriter 方法和 XmlWriter.Create 類別來建立XmlWriterSettings實例,以利用新功能。

例如,以下 Microsoft Visual C# 程式碼:

writer.Formatting = Formatting.Indented;
writer.WriteStartElement("root");
 writer.WriteAttributeString("xmlns","x",null,"urn:abc");
 writer.WriteStartElement("item");
 writer.WriteStartAttribute("href",null);
 writer.WriteString("#");
 writer.WriteQualifiedName("test","urn:abc");
 writer.WriteEndAttribute();
 writer.WriteEndElement();
 writer.WriteEndElement();
 writer.Close();

它會產生下列輸出:

<root xmlns:x="urn:abc">
 <item href="#x:test"/>
</root>

若 ns 映射到目前預設命名空間,則不會產生前綴。

在撰寫屬性值時,若 ns 找不到,此方法會產生前綴。 撰寫元素內容時,若 ns 找不到會拋出例外。

若此作者支援命名空間(Namespaces 設 true為 ),此方法也會根據 W3C XML 命名空間建議檢查名稱是否有效。

適用於