System.Xml.XmlWriter.WriteAttributeString 方法 (String, String, String, String)

方法描述

当在派生类中被重写时,写出具有指定的前缀、本地名称、命名空间 URI 和值的特性。

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

public void WriteAttributeString(
	string prefix,
	string localName,
	string ns,
	string value
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
prefix System-String 特性的命名空间前缀。
localName System-String 特性的本地名称。
ns System-String 特性的命名空间 URI。
value System-String 属性的值。
返回值 void

提示和注释

此方法写出具有用户定义的命名空间前缀的特性,并将其与给定的命名空间进行关联。 如果前缀为“xmlns”,则此方法也将此当做命名空间声明对待,并将声明的前缀与给定特性值中提供的命名空间 URI 进行关联。 在这种情况下,ns 参数可以为 null。

WriteAttributeString 执行以下操作:

如果特性值包含双引号或单引号,则分别用 " 和 ' 替换它们。

如果编写 xml:space 特性,则编写器验证该特性值是否有效。 (有效值为 preserve 或 default。)

如果编写 xml:lang 特性,则编写器不根据 W3C XML 1.0 建议验证该特性值是否有效。

System.Xml.XmlWriter.WriteAttributeString 方法 (String, String, String, String)例子

下面的示例使用 WriteAttributeString 方法编写命名空间声明。

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

public class Sample {

  public static void Main() {

     XmlWriter writer = null;

     writer = XmlWriter.Create("sampledata.xml");

     // Write the root element.
     writer.WriteStartElement("book");

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

     // Write the bk:ISBN="1-800-925" attribute.
     writer.WriteAttributeString("ISBN", "urn:book", "1-800-925");

     writer.WriteElementString("price", "19.95");

     // Write the close tag for the root element.
     writer.WriteEndElement();

     // Write the XML to file and close the writer.
     writer.Flush();
     writer.Close();  

  }
}

异常

异常 异常描述
InvalidOperationException 编写器的状态不是 WriteState.Element 或者编写器已关闭。
ArgumentException xml:space 或 xml:lang 特性值无效。
XmlException localName 或 ns 为 null。

命名空间

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