XmlTextWriter.WriteQualifiedName(String, String) Método

Definição

Escreve o nome qualificado no espaço de nomes. Este método procura o prefixo que está no âmbito do espaço de nomes dado.

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)

Parâmetros

localName
String

O nome local a escrever.

ns
String

O URI do namespace para associar ao nome.

Exceções

localName é null ou String.Empty.

localName não é um nome válido segundo a especificação W3C Namespaces.

Exemplos

O exemplo seguinte descreve uma parte de um esquema 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

Observações

Note

Recomendamos que crie instâncias XmlWriter usando o método XmlWriter.Create e a classe XmlWriterSettings para aproveitar a nova funcionalidade.

Por exemplo, o seguinte código Visual C# da Microsoft:

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();

Gera a seguinte saída:

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

Se ns for mapeado para o namespace padrão atual, não é gerado nenhum prefixo.

Ao escrever valores de atributos, este método gera um prefixo se ns não for encontrado. Ao escrever conteúdo elemental, lança uma exceção se ns não for encontrado.

Se este autor suportar namespaces (Namespaces está definido para true), este método também verifica se o nome é válido de acordo com a recomendação W3C Namespaces in XML.

Aplica-se a