XmlTextWriter.WriteQualifiedName(String, String) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
写出命名空间限定的名称。 此方法查找位于给定命名空间范围内的前缀。
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)
参数
- localName
- String
要写入的本地名称。
- ns
- String
与该名称关联的命名空间 URI。
例外
示例
以下示例写出 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
注解
备注
从 .NET Framework 2.0 开始,我们建议使用XmlWriter.Create方法和XmlWriterSettings类创建XmlWriter实例,以利用新功能。
例如,以下 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();
生成以下输出:
<root xmlns:x="urn:abc">
<item href="#x:test"/>
</root>
如果 ns
映射到当前默认命名空间,则不会生成任何前缀。
编写属性值时,如果未找到,此方法将生成前缀 ns
。 编写元素内容时,如果未 ns
找到,则会引发异常。
如果此编写器支持命名空间 (Namespaces 设置为 true
) ,此方法还会根据 XML 建议中的 W3C 命名空间检查名称是否有效。