XmlTextWriter.WriteQualifiedName(String, String) Metoda

Definicja

Zapisuje kwalifikowaną nazwę przestrzeni nazw. Ta metoda wyszukuje prefiks, który jest w zakresie dla danej przestrzeni nazw.

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)

Parametry

localName
String

Nazwa lokalna do zapisania.

ns
String

Identyfikator URI przestrzeni nazw do skojarzenia z nazwą.

Wyjątki

Parametr localName ma wartość null lub String.Empty.

localName nie jest prawidłową nazwą zgodnie ze specyfikacją przestrzeni nazw W3C.

Przykłady

Poniższy przykład zapisuje część schematu 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

Uwagi

Uwaga

Począwszy od .NET Framework 2.0, zalecamy utworzenie XmlWriter wystąpień przy użyciu metody i XmlWriterSettings klasy, aby korzystać z XmlWriter.Create nowych funkcji.

Na przykład następujący kod programu 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();  

Generuje następujące dane wyjściowe:

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

Jeśli ns mapuje na bieżącą domyślną przestrzeń nazw, nie jest generowany żaden prefiks.

Podczas pisania wartości atrybutów ta metoda generuje prefiks, jeśli ns nie zostanie znaleziony. Podczas pisania zawartości elementu zgłasza wyjątek, jeśli ns nie zostanie znaleziony.

Jeśli ten składnik zapisywania obsługuje przestrzenie nazw (Namespaces jest ustawiony na true), ta metoda sprawdza również, czy nazwa jest prawidłowa zgodnie z zaleceniami przestrzeni nazw W3C w rekomendacji XML.

Dotyczy