XmlWriter.WriteElementString Método

Definición

Escribe un elemento que contiene un valor de cadena.

Sobrecargas

Nombre Description
WriteElementString(String, String)

Escribe un elemento con el nombre y el valor locales especificados.

WriteElementString(String, String, String)

Escribe un elemento con el nombre local, el URI del espacio de nombres y el valor especificados.

WriteElementString(String, String, String, String)

Escribe un elemento con el prefijo, el nombre local, el URI del espacio de nombres y el valor especificados.

Comentarios

Para obtener la versión asincrónica de este método, vea WriteElementStringAsync.

WriteElementString(String, String)

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

Escribe un elemento con el nombre y el valor locales especificados.

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

Parámetros

localName
String

Nombre local del elemento.

value
String

El valor del elemento.

Excepciones

El localName valor es null o una cadena vacía.

O bien

Los valores de parámetro no son válidos.

Hay un carácter en el búfer que es un carácter XML válido, pero no es válido para la codificación de salida. Por ejemplo, si la codificación de salida es ASCII, solo debe usar caracteres del intervalo de 0 a 127 para los nombres de elemento y atributo. El carácter no válido puede estar en el argumento de este método o en un argumento de los métodos anteriores que se escriben en el búfer. Estos caracteres se escapan mediante referencias de entidad de caracteres siempre que sea posible (por ejemplo, en nodos de texto o valores de atributo). Sin embargo, no se permite la referencia de entidad de caracteres en nombres de elemento y atributo, comentarios, instrucciones de procesamiento o secciones de CDATA.

Se llamó a un XmlWriter método antes de que finalice una operación asincrónica anterior. En este caso, InvalidOperationException se produce con el mensaje "Una operación asincrónica ya está en curso".

Ejemplos

En el ejemplo siguiente se usan varios métodos de escritura para crear un fragmento XML.

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

Comentarios

Para obtener la versión asincrónica de este método, vea WriteElementStringAsync.

Se aplica a

WriteElementString(String, String, String)

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

Escribe un elemento con el nombre local, el URI del espacio de nombres y el valor especificados.

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

Parámetros

localName
String

Nombre local del elemento.

ns
String

Identificador URI del espacio de nombres que se va a asociar al elemento .

value
String

El valor del elemento.

Excepciones

El localName valor es null o una cadena vacía.

O bien

Los valores de parámetro no son válidos.

Hay un carácter en el búfer que es un carácter XML válido, pero no es válido para la codificación de salida. Por ejemplo, si la codificación de salida es ASCII, solo debe usar caracteres del intervalo de 0 a 127 para los nombres de elemento y atributo. El carácter no válido puede estar en el argumento de este método o en un argumento de los métodos anteriores que se escriben en el búfer. Estos caracteres se escapan mediante referencias de entidad de caracteres siempre que sea posible (por ejemplo, en nodos de texto o valores de atributo). Sin embargo, no se permite la referencia de entidad de caracteres en nombres de elemento y atributo, comentarios, instrucciones de procesamiento o secciones de CDATA.

Se llamó a un XmlWriter método antes de que finalice una operación asincrónica anterior. En este caso, InvalidOperationException se produce con el mensaje "Una operación asincrónica ya está en curso".

Ejemplos

En el ejemplo siguiente se usan varios métodos de escritura para crear un fragmento XML.

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

Comentarios

Para obtener la versión asincrónica de este método, vea WriteElementStringAsync.

Se aplica a

WriteElementString(String, String, String, String)

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

Escribe un elemento con el prefijo, el nombre local, el URI del espacio de nombres y el valor especificados.

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

Parámetros

prefix
String

Prefijo del elemento.

localName
String

Nombre local del elemento.

ns
String

Identificador URI del espacio de nombres del elemento.

value
String

El valor del elemento.

Excepciones

El localName valor es null o una cadena vacía.

O bien

Los valores de parámetro no son válidos.

Hay un carácter en el búfer que es un carácter XML válido, pero no es válido para la codificación de salida. Por ejemplo, si la codificación de salida es ASCII, solo debe usar caracteres del intervalo de 0 a 127 para los nombres de elemento y atributo. El carácter no válido puede estar en el argumento de este método o en un argumento de los métodos anteriores que se escriben en el búfer. Estos caracteres se escapan mediante referencias de entidad de caracteres siempre que sea posible (por ejemplo, en nodos de texto o valores de atributo). Sin embargo, no se permite la referencia de entidad de caracteres en nombres de elemento y atributo, comentarios, instrucciones de procesamiento o secciones de CDATA.

Se llamó a un XmlWriter método antes de que finalice una operación asincrónica anterior. En este caso, InvalidOperationException se produce con el mensaje "Una operación asincrónica ya está en curso".

Comentarios

Para obtener la versión asincrónica de este método, vea WriteElementStringAsync.

Se aplica a