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 和值的属性。

public:
 void WriteAttributeString(System::String ^ prefix, System::String ^ localName, System::String ^ ns, System::String ^ value);
public void WriteAttributeString (string prefix, string localName, string ns, string value);
public void WriteAttributeString (string? prefix, string localName, string? ns, string? value);
member this.WriteAttributeString : string * string * string * string -> unit
Public Sub WriteAttributeString (prefix As String, localName As String, ns As String, value As String)

参数

prefix
String

属性的命名空间前缀。

localName
String

属性的本地名称。

ns
String

属性的命名空间 URI。

value
String

属性的值。

例外

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

- 或 -

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

xml:spacexml:lang 属性值无效。

localNamensnull

示例

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

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
void main()
{
   XmlWriter^ writer = nullptr;
   writer = XmlWriter::Create( L"sampledata.xml" );
   
   // Write the root element.
   writer->WriteStartElement( L"book" );
   
   // Write the xmlns:bk="urn:book" namespace declaration.
   writer->WriteAttributeString( L"xmlns", L"bk", nullptr, L"urn:book" );
   
   // Write the bk:ISBN="1-800-925" attribute.
   writer->WriteAttributeString( L"ISBN", L"urn:book", L"1-800-925" );
   writer->WriteElementString( L"price", L"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();
}
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();
  }
}
Imports System.IO
Imports System.Xml

Public Class Sample 

  Public Shared Sub Main() 
 
     Dim writer As XmlWriter = Nothing

     writer = XmlWriter.Create("sampledata.xml")
        
     ' Write the root element.
     writer.WriteStartElement("book")

     ' Write the xmlns:bk="urn:book" namespace declaration.
     writer.WriteAttributeString("xmlns","bk", Nothing,"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()  

  End Sub
End Class

注解

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

WriteAttributeString 执行下列操作:

  • 如果属性值包含双引号或单引号,则分别用 &quot;&apos; 替换它们。

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

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

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

适用于

WriteAttributeString(String, String, String)

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

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

public:
 void WriteAttributeString(System::String ^ localName, System::String ^ ns, System::String ^ value);
public void WriteAttributeString (string localName, string ns, string value);
public void WriteAttributeString (string localName, string? ns, string? value);
member this.WriteAttributeString : string * string * string -> unit
Public Sub WriteAttributeString (localName As String, ns As String, value As String)

参数

localName
String

属性的本地名称。

ns
String

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

value
String

属性的值。

例外

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

- 或 -

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

xml:spacexml:lang 属性值无效。

示例

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

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
void main()
{
   XmlWriter^ writer = nullptr;
   writer = XmlWriter::Create( L"sampledata.xml" );
   
   // Write the root element.
   writer->WriteStartElement( L"book" );
   
   // Write the xmlns:bk="urn:book" namespace declaration.
   writer->WriteAttributeString( L"xmlns", L"bk", nullptr, L"urn:book" );
   
   // Write the bk:ISBN="1-800-925" attribute.
   writer->WriteAttributeString( L"ISBN", L"urn:book", L"1-800-925" );
   writer->WriteElementString( L"price", L"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();
}
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();
  }
}
Imports System.IO
Imports System.Xml

Public Class Sample 

  Public Shared Sub Main() 
 
     Dim writer As XmlWriter = Nothing

     writer = XmlWriter.Create("sampledata.xml")
        
     ' Write the root element.
     writer.WriteStartElement("book")

     ' Write the xmlns:bk="urn:book" namespace declaration.
     writer.WriteAttributeString("xmlns","bk", Nothing,"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()  

  End Sub
End Class

注解

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

WriteAttributeString 执行下列操作:

  • 如果属性值包含双引号或单引号,则分别用 &quot;&apos; 替换它们。

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

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

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

适用于

WriteAttributeString(String, String)

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

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

public:
 void WriteAttributeString(System::String ^ localName, System::String ^ value);
public void WriteAttributeString (string localName, string value);
public void WriteAttributeString (string localName, string? value);
member this.WriteAttributeString : string * string -> unit
Public Sub WriteAttributeString (localName As String, value As String)

参数

localName
String

属性的本地名称。

value
String

属性的值。

例外

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

- 或 -

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

xml:spacexml:lang 属性值无效。

示例

以下示例写出一本书。

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
public ref class Sample
{
private:
   static String^ m_Document = L"sampledata.xml";

public:
   static void Main()
   {
      XmlWriter^ writer = nullptr;
      try
      {
         XmlWriterSettings^ settings = gcnew XmlWriterSettings;
         settings->Indent = true;
         writer = XmlWriter::Create( m_Document,settings );
         writer->WriteComment( L"sample XML fragment" );
         
         // Write an element (this one is the root).
         writer->WriteStartElement( L"book" );
         
         // Write the namespace declaration.
         writer->WriteAttributeString( L"xmlns", L"bk", nullptr, L"urn:samples" );
         
         // Write the genre attribute.
         writer->WriteAttributeString( L"genre", L"novel" );
         
         // Write the title.
         writer->WriteStartElement( L"title" );
         writer->WriteString( L"The Handmaid's Tale" );
         writer->WriteEndElement();
         
         // Write the price.
         writer->WriteElementString( L"price", L"19.95" );
         
         // Lookup the prefix and write the ISBN element.
         String^ prefix = writer->LookupPrefix( L"urn:samples" );
         writer->WriteStartElement( prefix, L"ISBN", L"urn:samples" );
         writer->WriteString( L"1-861003-78" );
         writer->WriteEndElement();
         
         // Write the style element (shows a different way to handle prefixes).
         writer->WriteElementString( L"style", L"urn:samples", L"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 != nullptr )
                  writer->Close();
      }

   }

};

void main()
{
   Sample::Main();
}
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();
     }
   }
 }
Option Strict
Option Explicit

Imports System.IO
Imports System.Xml

Public Class Sample
    Private Shared m_Document As String = "sampledata.xml"
    
    Public Shared Sub Main()
        Dim writer As XmlWriter = Nothing
        
      Try

        Dim settings As XmlWriterSettings = 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", Nothing, "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.
        Dim prefix As String = 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 Not (writer Is Nothing) Then
                writer.Close()
            End If
        End Try

    End Sub
End Class

注解

WriteAttributeString 执行下列操作:

  • 如果属性值包含双引号或单引号,则分别用 &quot;&apos; 替换它们。

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

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

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

适用于