System.Xml.XmlTextWriter.WriteQualifiedName 方法

方法描述

写出命名空间限定的名称。 此方法查找位于给定命名空间范围内的前缀。

语法定义(C# System.Xml.XmlTextWriter.WriteQualifiedName 方法 的用法)

public override void WriteQualifiedName(
	string localName,
	string ns
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
localName System-String 要编写的本地名称。
ns System-String 与该名称关联的命名空间 URI。
返回值 void

提示和注释

注意

在 .NET Framework 2.0 版 版本中,推荐的做法是使用 XmlWriter.Create 方法和 XmlWriterSettings 类创建 XmlWriter 实例。 这使您可以充分利用此版本中引入的所有新功能。 有关更多信息,请参见 创建 XML 编写器。

例如,下面的 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();

生成下列输出:

复制

如果 ns 映射到当前默认命名空间,则不生成前缀。

当编写特性值时,如果未找到 ns,则此方法生成前缀。 当编写元素内容时,如果未找到 ns,则它引发异常。

如果此编写器支持命名空间(Namespaces 设置为 true),则此方法还根据 W3C 有关 XML 中命名空间的建议 (http://www.w3.org/TR/REC-xml-names) 检查此名称是否有效。

System.Xml.XmlTextWriter.WriteQualifiedName 方法例子

下面的示例写出 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);

  }

}

异常

异常 异常描述
ArgumentException localName 为 null 或 String.Empty。 根据 W3C 命名空间规范,localName 不是有效的名称。

命名空间

namespace: System.Xml

程序集: System.Xml(在 System.Xml.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。