XmlWriter.WriteAttributeString Metoda

Definice

Při přepsání v odvozené třídě zapíše atribut se zadanou hodnotou.

Přetížení

WriteAttributeString(String, String, String, String)

Při přepsání v odvozené třídě zapíše atribut se zadanou předponou, místním názvem, identifikátorem URI oboru názvů a hodnotou.

WriteAttributeString(String, String, String)

Při přepsání v odvozené třídě zapíše atribut se zadaným místním názvem, identifikátorem URI oboru názvů a hodnotou.

WriteAttributeString(String, String)

Při přepsání v odvozené třídě zapíše atribut se zadaným místním názvem a hodnotou.

Poznámky

Asynchronní verzi této metody najdete v tématu WriteElementStringAsync.

WriteAttributeString(String, String, String, String)

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

Při přepsání v odvozené třídě zapíše atribut se zadanou předponou, místním názvem, identifikátorem URI oboru názvů a hodnotou.

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)

Parametry

prefix
String

Předpona oboru názvů atributu.

localName
String

Místní název atributu.

ns
String

Identifikátor URI oboru názvů atributu.

value
String

Hodnota atributu.

Výjimky

Stav zapisovače není WriteState.Element nebo je zapisovač uzavřen.

-nebo-

Před XmlWriter dokončením předchozí asynchronní operace byla volána metoda . V tomto případě InvalidOperationException je vyvolán se zprávou "Asynchronní operace již probíhá.".

Hodnota atributu xml:space nebo xml:lang je neplatná.

Hodnota localName nebo ns je null.

Příklady

Následující příklad používá metodu WriteAttributeString k zápisu deklarace oboru názvů.

#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

Poznámky

Tato metoda vypíše atribut s předponou oboru názvů definovanou uživatelem a přidruží ho k danému oboru názvů. Pokud je předpona "xmlns", pak tato metoda také považuje za deklaraci oboru názvů a přidruží deklarovanou předponu k identifikátoru URI oboru názvů zadanému v dané hodnotě atributu. V tomto případě ns může být nullargumentem .

WriteAttributeString provede následující:

  • Pokud hodnota atributu obsahuje dvojité nebo jednoduché uvozovky, nahradí &quot; se za a &apos; v uvedeném pořadí.

  • Při zápisu atributu xml:space zapisovač ověří, že hodnota atributu je platná. (Platné hodnoty jsou preserve nebo default.)

  • Při zápisu xml:lang atributu zapisovač neověřuje, zda je hodnota atributu platná podle doporučení W3C XML 1.0.

Asynchronní verzi této metody najdete v tématu WriteAttributeStringAsync.

Platí pro

WriteAttributeString(String, String, String)

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

Při přepsání v odvozené třídě zapíše atribut se zadaným místním názvem, identifikátorem URI oboru názvů a hodnotou.

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)

Parametry

localName
String

Místní název atributu.

ns
String

Identifikátor URI oboru názvů, který se má přidružit k atributu .

value
String

Hodnota atributu.

Výjimky

Stav zapisovače není WriteState.Element nebo je zapisovač uzavřen.

-nebo-

Před XmlWriter dokončením předchozí asynchronní operace byla volána metoda . V tomto případě InvalidOperationException je vyvolán se zprávou "Asynchronní operace již probíhá.".

Hodnota atributu xml:space nebo xml:lang je neplatná.

Příklady

Následující příklad používá metodu WriteAttributeString k zápisu deklarace oboru názvů.

#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

Poznámky

Tato metoda vypíše atribut s předponou oboru názvů definovanou uživatelem a přidruží ho k danému oboru názvů. Pokud localName je "xmlns", pak tato metoda také považuje za deklaraci oboru názvů. V tomto případě ns může být nullargument .

WriteAttributeString provede následující:

  • Pokud hodnota atributu obsahuje dvojité nebo jednoduché uvozovky, nahradí &quot; se za a &apos; v uvedeném pořadí.

  • Při zápisu atributu xml:space zapisovač ověří, že hodnota atributu je platná. (Platné hodnoty jsou preserve nebo default.)

  • Při zápisu xml:lang atributu zapisovač neověřuje, zda je hodnota atributu platná podle doporučení W3C XML 1.0.

Asynchronní verzi této metody najdete v tématu WriteAttributeStringAsync.

Platí pro

WriteAttributeString(String, String)

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

Při přepsání v odvozené třídě zapíše atribut se zadaným místním názvem a hodnotou.

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)

Parametry

localName
String

Místní název atributu.

value
String

Hodnota atributu.

Výjimky

Stav zapisovače není WriteState.Element nebo je zapisovač uzavřen.

-nebo-

Před XmlWriter dokončením předchozí asynchronní operace byla volána metoda . V tomto případě InvalidOperationException je vyvolán se zprávou "Asynchronní operace již probíhá.".

Hodnota atributu xml:space nebo xml:lang je neplatná.

Příklady

Následující příklad zapíše knihu.

#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

Poznámky

WriteAttributeString provede následující:

  • Pokud hodnota atributu obsahuje dvojité nebo jednoduché uvozovky, nahradí &quot; se za a &apos; v uvedeném pořadí.

  • Při zápisu atributu xml:space zapisovač ověří, že hodnota atributu je platná. (Platné hodnoty jsou preserve nebo default.)

  • Při zápisu xml:lang atributu zapisovač neověřuje, zda je hodnota atributu platná podle doporučení W3C XML 1.0.

Asynchronní verzi této metody najdete v tématu WriteAttributeStringAsync.

Platí pro