XmlTextWriter.WriteQualifiedName(String, String) Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Écrit le nom qualifié de l’espace de noms. Cette méthode recherche le préfixe qui est dans l’étendue de l’espace de noms donné.
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)
Paramètres
- localName
- String
Nom local à écrire.
- ns
- String
URI d’espace de noms à associer au nom.
Exceptions
localName est null ou String.Empty.
localName n’est pas un nom valide selon la spécification des espaces de noms W3C.
Exemples
L’exemple suivant écrit une partie d’un schéma 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
Remarques
Note
Nous vous recommandons de créer XmlWriter instances en utilisant la méthode XmlWriter.Create et la classe XmlWriterSettings pour tirer parti de nouvelles fonctionnalités.
Par exemple, le code Microsoft Visual C# suivant :
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();
Voici la sortie générée :
<root xmlns:x="urn:abc">
<item href="#x:test"/>
</root>
Si ns la carte est mappée à l’espace de noms par défaut actuel, aucun préfixe n’est généré.
Lors de l’écriture de valeurs d’attribut, cette méthode génère un préfixe s’il ns est introuvable. Lors de l’écriture du contenu de l’élément, elle lève une exception si ns elle est introuvable.
Si cet enregistreur prend en charge les espaces de noms (Namespaces est défini sur true), cette méthode vérifie également que le nom est valide en fonction des espaces de noms W3C dans la recommandation XML.