XmlTextWriter.WriteQualifiedName(String, String) Metode
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Menulis nama yang memenuhi syarat namespace layanan. Metode ini mencari awalan yang berada dalam cakupan untuk namespace yang diberikan.
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)
Parameter
- localName
- String
Nama lokal untuk ditulis.
- ns
- String
URI namespace untuk dikaitkan dengan nama.
Pengecualian
localName adalah baik null maupun String.Empty.
localName bukan nama yang valid sesuai dengan spesifikasi Namespace W3C.
Contoh
Contoh berikut menulis sebagian skema 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
Keterangan
Nota
Kami menyarankan agar Anda membuat XmlWriter instans dengan menggunakan XmlWriter.Create metode dan XmlWriterSettings kelas untuk memanfaatkan fungsionalitas baru.
Misalnya, kode Microsoft Visual C# berikut:
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();
Menghasilkan output berikut:
<root xmlns:x="urn:abc">
<item href="#x:test"/>
</root>
Jika ns memetakan ke namespace default saat ini, tidak ada awalan yang dihasilkan.
Saat menulis nilai atribut, metode ini menghasilkan awalan jika ns tidak ditemukan. Saat menulis konten elemen, ia melemparkan pengecualian jika ns tidak ditemukan.
Jika penulis ini mendukung namespace layanan (Namespaces diatur ke true), metode ini juga memeriksa apakah nama tersebut valid sesuai dengan Namespace W3C dalam rekomendasi XML.