System.Xml.XmlTextWriter.WriteStartAttribute 方法 (String, String, String)

方法描述

编写特性的起始内容。

语法定义(C# System.Xml.XmlTextWriter.WriteStartAttribute 方法 (String, String, String) 的用法)

public override void WriteStartAttribute(
	string prefix,
	string localName,
	string ns
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
prefix System-String 特性的 Namespace 前缀。
localName System-String 特性的 LocalName。
ns System-String 特性的 NamespaceURI
返回值 void

提示和注释

注意

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

这是 WriteAttributeString 更高级的版本,允许您使用多种写方法(例如 WriteString)编写特性值。

System.Xml.XmlTextWriter.WriteStartAttribute 方法 (String, String, String)例子

下面的示例写出关于书的代码。

using System;
using System.IO;
using System.Xml;

public class Sample
{
  private const string filename = "sampledata.xml";

  public static void Main()
  {

     XmlTextWriter writer = new XmlTextWriter (filename, null);
     //Use indenting for readability.
     writer.Formatting = Formatting.Indented;

     writer.WriteComment("sample XML fragment");

     //Write an element (this one is the root).
     writer.WriteStartElement("bookstore");

     //Write the namespace declaration.
     writer.WriteAttributeString("xmlns", "bk", null, "urn:samples");

     writer.WriteStartElement("book");

     //Lookup the prefix and then write the ISBN attribute.
     string prefix = writer.LookupPrefix("urn:samples");
     writer.WriteStartAttribute(prefix, "ISBN", "urn:samples");
     writer.WriteString("1-861003-78");
     writer.WriteEndAttribute();     

     //Write the title.
     writer.WriteStartElement("title");
     writer.WriteString("The Handmaid's Tale");
     writer.WriteEndElement();

     //Write the price.
     writer.WriteElementString("price", "19.95");

     //Write the style element.
     writer.WriteStartElement(prefix, "style", "urn:samples");
     writer.WriteString("hardcover");
     writer.WriteEndElement();

     //Write the end tag for the book element.
     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。

命名空间

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 系统要求。