共用方式為


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。

例外狀況

localNamenullString.Empty

根據 W3C 命名空間規格,localName 不是有效的名稱。

範例

下列範例會寫出 XSD 架構的一部分。

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlTextWriter^ writer = nullptr;
   String^ filename = "sampledata.xml";
   writer = gcnew XmlTextWriter( filename, nullptr );
   
   // Use indenting for readability.
   writer->Formatting = Formatting::Indented;
   
   // Write the root element.
   writer->WriteStartElement( "schema" );
   
   // Write the namespace declarations.
   writer->WriteAttributeString( "xmlns", nullptr, "http://www.w3.org/2001/XMLSchema" );
   writer->WriteAttributeString( "xmlns", "po", nullptr, "http://contoso.com/po" );
   writer->WriteStartElement( "element" );
   writer->WriteAttributeString( "name", "purchaseOrder" );
   
   // Write the type attribute.
   writer->WriteStartAttribute( nullptr, "type", nullptr );
   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 = gcnew 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 );
}
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

備註

注意

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

例如,下列 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) ,此方法也會根據 XML 建議中的 W3C 命名空間檢查名稱是否有效。

適用於