XmlWriter.WriteAttributeString 方法

定义

当在派生类中被重写时,写入具有指定值的属性。

重载

WriteAttributeString(String, String, String, String)

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

WriteAttributeString(String, String, String)

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

WriteAttributeString(String, String)

当在派生类中被重写时,写出具有指定的本地名称和值的属性。

注解

有关此方法的异步版本,请参阅 WriteElementStringAsync

WriteAttributeString(String, String, String, String)

Source:
XmlWriter.cs
Source:
XmlWriter.cs
Source:
XmlWriter.cs

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

C#
public void WriteAttributeString (string prefix, string localName, string ns, string value);
C#
public void WriteAttributeString (string? prefix, string localName, string? ns, string? value);

参数

prefix
String

属性的命名空间前缀。

localName
String

属性的本地名称。

ns
String

属性的命名空间 URI。

value
String

属性的值。

例外

编写器的状态不是 WriteState.Element 或者编写器已关闭。

- 或 -

在上一次异步操作完成之前调用了 XmlWriter 方法。 在此情况下,会引发 InvalidOperationException 并显示消息“异步操作已在进行中。”

xml:spacexml:lang 属性值无效。

localNamensnull

示例

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

C#
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();
  }
}

注解

此方法写出具有用户定义的命名空间前缀的属性,并将其与给定的命名空间相关联。 如果前缀为“xmlns”,则此方法还会将其视为命名空间声明,并将声明的前缀与给定属性值中提供的命名空间 URI 相关联。 在这种情况下, ns 参数可以是 null

WriteAttributeString 执行下列操作:

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

  • 如果编写属性 xml:space ,编写器将验证属性值是否有效。 (有效值为 preservedefault.)

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

有关此方法的异步版本,请参阅 WriteAttributeStringAsync

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

WriteAttributeString(String, String, String)

Source:
XmlWriter.cs
Source:
XmlWriter.cs
Source:
XmlWriter.cs

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

C#
public void WriteAttributeString (string localName, string ns, string value);
C#
public void WriteAttributeString (string localName, string? ns, string? value);

参数

localName
String

属性的本地名称。

ns
String

与属性关联的命名空间 URI。

value
String

属性的值。

例外

编写器的状态不是 WriteState.Element 或者编写器已关闭。

- 或 -

在上一次异步操作完成之前调用了 XmlWriter 方法。 在此情况下,会引发 InvalidOperationException 并显示消息“异步操作已在进行中。”

xml:spacexml:lang 属性值无效。

示例

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

C#
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();
  }
}

注解

此方法写出具有用户定义的命名空间前缀的属性,并将其与给定的命名空间相关联。 如果 localName 为“xmlns”,则此方法也将其视为命名空间声明。 在这种情况下, ns 参数可以是 null

WriteAttributeString 执行下列操作:

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

  • 如果编写属性 xml:space ,编写器将验证属性值是否有效。 (有效值为 preservedefault.)

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

有关此方法的异步版本,请参阅 WriteAttributeStringAsync

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

WriteAttributeString(String, String)

Source:
XmlWriter.cs
Source:
XmlWriter.cs
Source:
XmlWriter.cs

当在派生类中被重写时,写出具有指定的本地名称和值的属性。

C#
public void WriteAttributeString (string localName, string value);
C#
public void WriteAttributeString (string localName, string? value);

参数

localName
String

属性的本地名称。

value
String

属性的值。

例外

编写器的状态不是 WriteState.Element 或者编写器已关闭。

- 或 -

在上一次异步操作完成之前调用了 XmlWriter 方法。 在此情况下,会引发 InvalidOperationException 并显示消息“异步操作已在进行中。”

xml:spacexml:lang 属性值无效。

示例

以下示例写出一本书。

C#
using System;
using System.IO;
using System.Xml;

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

   public static void Main() {

      XmlWriter writer = null;

      try {

        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        writer = XmlWriter.Create (m_Document, settings);

        writer.WriteComment("sample XML fragment");

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

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

        // Write the genre attribute.
        writer.WriteAttributeString("genre", "novel");

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

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

        // Lookup the prefix and write the ISBN element.
        string prefix = writer.LookupPrefix("urn:samples");
        writer.WriteStartElement(prefix, "ISBN", "urn:samples");
        writer.WriteString("1-861003-78");
        writer.WriteEndElement();

        // Write the style element (shows a different way to handle prefixes).
        writer.WriteElementString("style", "urn:samples", "hardcover");

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

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

      finally {
        if (writer != null)
           writer.Close();
     }
   }
 }

注解

WriteAttributeString 执行下列操作:

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

  • 如果编写属性 xml:space ,编写器将验证属性值是否有效。 (有效值为 preservedefault.)

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

有关此方法的异步版本,请参阅 WriteAttributeStringAsync

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0