XmlWriter.WriteAttributeString Método

Definição

Quando sobrescrito numa classe derivada, escreve um atributo com o valor especificado.

Sobrecargas

Name Description
WriteAttributeString(String, String, String, String)

Quando sobreposto numa classe derivada, escreve o atributo com o prefixo especificado, nome local, URI do espaço de nomes e valor.

WriteAttributeString(String, String, String)

Quando sobrescrito numa classe derivada, escreve um atributo com o nome local especificado, o URI do espaço de nomes e o valor.

WriteAttributeString(String, String)

Quando sobrescrito numa classe derivada, escreve o atributo com o nome local e o valor especificados.

Observações

Para a versão assíncrona deste método, veja WriteElementStringAsync.

WriteAttributeString(String, String, String, String)

Origem:
XmlWriter.cs
Origem:
XmlWriter.cs
Origem:
XmlWriter.cs
Origem:
XmlWriter.cs
Origem:
XmlWriter.cs

Quando sobreposto numa classe derivada, escreve o atributo com o prefixo especificado, nome local, URI do espaço de nomes e valor.

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)

Parâmetros

prefix
String

O prefixo de namespace do atributo.

localName
String

O nome local do atributo.

ns
String

O URI do namespace do atributo.

value
String

O valor do atributo.

Exceções

O estado do escritor não WriteState.Element é ou o escritor está fechado.

-ou-

Um XmlWriter método era chamado antes de uma operação assíncrona anterior terminar. Neste caso, InvalidOperationException é lançado com a mensagem "Uma operação assíncrona já está em curso."

O xml:space valor do atributo ou xml:lang é inválido.

O localName ou ns é null.

Exemplos

O exemplo seguinte utiliza o WriteAttributeString método para escrever uma declaração de namespace.

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

Observações

Este método escreve o atributo com um prefixo de namespace definido pelo utilizador e associa-o ao namespace dado. Se o prefixo for "xmlns", então este método também trata isto como uma declaração de espaço de nomes e associa o prefixo declarado ao URI do espaço de nomes fornecido no valor do atributo dado. Neste caso, o ns argumento pode ser null.

WriteAttributeString faz o seguinte:

  • Se o valor do atributo incluir aspas duplas ou simples, estas são substituídas por " e ' respetivamente.

  • Se escrever um xml:space atributo, o autor verifica se o valor do atributo é válido. (Os valores válidos são preserve ou default.)

  • Ao escrever um xml:lang atributo, o autor não verifica se o valor do atributo é válido de acordo com a recomendação XML 1.0 do W3C.

Para a versão assíncrona deste método, veja WriteAttributeStringAsync.

Aplica-se a

WriteAttributeString(String, String, String)

Origem:
XmlWriter.cs
Origem:
XmlWriter.cs
Origem:
XmlWriter.cs
Origem:
XmlWriter.cs
Origem:
XmlWriter.cs

Quando sobrescrito numa classe derivada, escreve um atributo com o nome local especificado, o URI do espaço de nomes e o valor.

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)

Parâmetros

localName
String

O nome local do atributo.

ns
String

O URI do namespace para associar ao atributo.

value
String

O valor do atributo.

Exceções

O estado do escritor não WriteState.Element é ou o escritor está fechado.

-ou-

Um XmlWriter método era chamado antes de uma operação assíncrona anterior terminar. Neste caso, InvalidOperationException é lançado com a mensagem "Uma operação assíncrona já está em curso."

O xml:space valor do atributo ou xml:lang é inválido.

Exemplos

O exemplo seguinte utiliza o WriteAttributeString método para escrever uma declaração de namespace.

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

Observações

Este método escreve o atributo com um prefixo de namespace definido pelo utilizador e associa-o ao namespace dado. Se localName for "xmlns", então este método também trata isto como uma declaração de espaço de nomes. Neste caso, o argumento ns pode ser null.

WriteAttributeString faz o seguinte:

  • Se o valor do atributo incluir aspas duplas ou simples, estas são substituídas por " e ' respetivamente.

  • Se escrever um xml:space atributo, o autor verifica se o valor do atributo é válido. (Os valores válidos são preserve ou default.)

  • Ao escrever um xml:lang atributo, o autor não verifica se o valor do atributo é válido de acordo com a recomendação XML 1.0 do W3C.

Para a versão assíncrona deste método, veja WriteAttributeStringAsync.

Aplica-se a

WriteAttributeString(String, String)

Origem:
XmlWriter.cs
Origem:
XmlWriter.cs
Origem:
XmlWriter.cs
Origem:
XmlWriter.cs
Origem:
XmlWriter.cs

Quando sobrescrito numa classe derivada, escreve o atributo com o nome local e o valor especificados.

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)

Parâmetros

localName
String

O nome local do atributo.

value
String

O valor do atributo.

Exceções

O estado do escritor não WriteState.Element é ou o escritor está fechado.

-ou-

Um XmlWriter método era chamado antes de uma operação assíncrona anterior terminar. Neste caso, InvalidOperationException é lançado com a mensagem "Uma operação assíncrona já está em curso."

O xml:space valor do atributo ou xml:lang é inválido.

Exemplos

O exemplo seguinte escreve um livro.

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

Observações

WriteAttributeString faz o seguinte:

  • Se o valor do atributo incluir aspas duplas ou simples, estas são substituídas por " e ' respetivamente.

  • Se escrever um xml:space atributo, o autor verifica se o valor do atributo é válido. (Os valores válidos são preserve ou default.)

  • Ao escrever um xml:lang atributo, o autor não verifica se o valor do atributo é válido de acordo com a recomendação XML 1.0 do W3C.

Para a versão assíncrona deste método, veja WriteAttributeStringAsync.

Aplica-se a