XmlTextWriter.LookupPrefix(String) 方法

定義

傳回最接近命名空間 URI 在目前命名空間範圍中定義的前置詞。

public:
 override System::String ^ LookupPrefix(System::String ^ ns);
public override string? LookupPrefix (string ns);
public override string LookupPrefix (string ns);
override this.LookupPrefix : string -> string
Public Overrides Function LookupPrefix (ns As String) As String

參數

ns
String

想要尋找其前置詞的命名空間 URI。

傳回

String

符合的前置詞。 如果在目前範圍中找不到符合的命名空間 URI,則為 null

例外狀況

nsnullString.Empty

範例

下列範例會寫出書籍。

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   String^ filename = "sampledata.xml";
   XmlTextWriter^ writer = gcnew XmlTextWriter( filename, nullptr );
   
   //Use indenting for readability.
   writer->Formatting = Formatting::Indented;
   writer->WriteComment( "sample XML fragment" );
   
   //Write an element (this one is the root).
   writer->WriteStartElement( "bookstore" );
   
   //Write the namespace declaration.
   writer->WriteAttributeString( "xmlns", "bk", nullptr, "urn:samples" );
   writer->WriteStartElement( "book" );
   
   //Lookup the prefix and then write the ISBN attribute.
   String^ prefix = writer->LookupPrefix( "urn:samples" );
   writer->WriteStartAttribute( prefix, "ISBN", "urn:samples" );
   writer->WriteString( "1-861003-78" );
   writer->WriteEndAttribute();
   
   //Write the title.
   writer->WriteStartElement( "title" );
   writer->WriteString( "The Handmaid's Tale" );
   writer->WriteEndElement();
   
   //Write the price.
   writer->WriteElementString( "price", "19.95" );
   
   //Write the style element.
   writer->WriteStartElement( prefix, "style", "urn:samples" );
   writer->WriteString( "hardcover" );
   writer->WriteEndElement();
   
   //Write the end tag for the book element.
   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 = new XmlTextWriter (filename, null);
     //Use indenting for readability.
     writer.Formatting = Formatting.Indented;

     writer.WriteComment("sample XML fragment");

     //Write an element (this one is the root).
     writer.WriteStartElement("bookstore");

     //Write the namespace declaration.
     writer.WriteAttributeString("xmlns", "bk", null, "urn:samples");

     writer.WriteStartElement("book");

     //Lookup the prefix and then write the ISBN attribute.
     string prefix = writer.LookupPrefix("urn:samples");
     writer.WriteStartAttribute(prefix, "ISBN", "urn:samples");
     writer.WriteString("1-861003-78");
     writer.WriteEndAttribute();

     //Write the title.
     writer.WriteStartElement("title");
     writer.WriteString("The Handmaid's Tale");
     writer.WriteEndElement();

     //Write the price.
     writer.WriteElementString("price", "19.95");

     //Write the style element.
     writer.WriteStartElement(prefix, "style", "urn:samples");
     writer.WriteString("hardcover");
     writer.WriteEndElement();

     //Write the end tag for the book element.
     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 Strict
Option Explicit

Imports System.IO
Imports System.Xml

Public Class Sample
    Private Shared filename As String = "sampledata.xml"
    Public Shared Sub Main()
        
        Dim writer As New XmlTextWriter(filename, Nothing)
        'Use indenting for readability.
        writer.Formatting = Formatting.Indented
        
        writer.WriteComment("sample XML fragment")
        
        'Write an element (this one is the root).
        writer.WriteStartElement("bookstore")
        
        'Write the namespace declaration.
        writer.WriteAttributeString("xmlns", "bk", Nothing, "urn:samples")
        
        writer.WriteStartElement("book")
        
        'Lookup the prefix and then write the ISBN attribute.
        Dim prefix As String = writer.LookupPrefix("urn:samples")
        writer.WriteStartAttribute(prefix, "ISBN", "urn:samples")
        writer.WriteString("1-861003-78")
        writer.WriteEndAttribute()
        
        'Write the title.
        writer.WriteStartElement("title")
        writer.WriteString("The Handmaid's Tale")
        writer.WriteEndElement()
        
        'Write the price.
        writer.WriteElementString("price", "19.95")
        
        'Write the style element.
        writer.WriteStartElement(prefix, "style", "urn:samples")
        writer.WriteString("hardcover")
        writer.WriteEndElement()
        
        'Write the end tag for the book element.
        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 實例,以利用新功能。

適用於