Freigeben über


XmlWriter.WriteComment-Methode

Schreibt beim Überschreiben in einer abgeleiteten Klasse den Kommentar <!--...--> mit dem angegebenen Text.

Namespace: System.Xml
Assembly: System.Xml (in system.xml.dll)

Syntax

'Declaration
Public MustOverride Sub WriteComment ( _
    text As String _
)
'Usage
Dim instance As XmlWriter
Dim text As String

instance.WriteComment(text)
public abstract void WriteComment (
    string text
)
public:
virtual void WriteComment (
    String^ text
) abstract
public abstract void WriteComment (
    String text
)
public abstract function WriteComment (
    text : String
)

Parameter

  • text
    Text, der in den Kommentar eingefügt werden soll.

Ausnahmen

Ausnahmetyp Bedingung

ArgumentException

Der Text ergibt kein wohlgeformtes XML-Dokument.

Hinweise

Wenn text entweder NULL (Nothing in Visual Basic) oder String.Empty ist, schreibt diese Methode einen Kommentar ohne Dateninhalt, z. B. <!---->.

Wenn text eine ungültige Sequenz von zwei Gedankenstrichen "--" enthält, kann der XmlWriter entweder eine ArgumentException auslösen (XmlTextWriter-Objekte) oder einen Leerraum zwischen den Gedankenstrichen einfügen "- -", sodass der Text ein gültiger XML-Kommentar ist (von der Create-Methode erstellte XmlWriter-Objekte).

Beispiel

Im folgenden Beispiel wird eine XML-Datei geschrieben, die ein Buch darstellt.

Option Strict
Option Explicit

Imports System
Imports System.IO
Imports System.Xml

Public Class Sample
    Private Const filename As String = "sampledata.xml"
    
  Public Shared Sub Main()

     Dim settings As XmlWriterSettings = new XmlWriterSettings()
     settings.Indent = true
     Dim writer As XmlWriter = XmlWriter.Create(filename, settings)
        
     ' Write the Processing Instruction node.
     Dim PItext As String = "type=""text/xsl"" href=""book.xsl"""
     writer.WriteProcessingInstruction("xml-stylesheet", PItext)
        
     'Write the DocumentType node.
     writer.WriteDocType("book", Nothing, Nothing, "<!ENTITY h ""hardcover"">")
        
     ' Write a Comment node.
     writer.WriteComment("sample XML")
        
     ' Write the root element.
     writer.WriteStartElement("book")
        
     ' Write the genre attribute
     writer.WriteAttributeString("genre", "novel")
        
     ' Write the ISBN attribute.
     writer.WriteAttributeString("ISBN", "1-8630-014")
        
     ' Write the title.
     writer.WriteElementString("title", "The Handmaid's Tale")
        
     ' Write the style element.
     writer.WriteStartElement("style")
     writer.WriteEntityRef("h")
     writer.WriteEndElement()
       
     ' Write the price.
     writer.WriteElementString("price", "19.95")
        
     ' Write CDATA.
     writer.WriteCData("Prices 15% off!!")
        
     ' Write the close tag for the root element.
     writer.WriteEndElement()
        
     writer.WriteEndDocument()
        
     ' Write the XML to file and close the writer
     writer.Flush()
     writer.Close()
        
    End Sub 'Main 
End Class 'Sample
using System;
using System.IO;
using System.Xml;

public class Sample {

  private const string filename = "sampledata.xml";

  public static void Main() {
  
     XmlWriterSettings settings = new XmlWriterSettings();
     settings.Indent = true;
     XmlWriter writer = XmlWriter.Create(filename, settings);

     // Write the Processing Instruction node.
     String PItext="type=\"text/xsl\" href=\"book.xsl\"";
     writer.WriteProcessingInstruction("xml-stylesheet", PItext);

     // Write the DocumentType node.
     writer.WriteDocType("book", null , null, "<!ENTITY h \"hardcover\">");
        
     // Write a Comment node.
     writer.WriteComment("sample XML");
    
     // Write the root element.
     writer.WriteStartElement("book");

     // Write the genre attribute.
     writer.WriteAttributeString("genre", "novel");
    
     // Write the ISBN attribute.
     writer.WriteAttributeString("ISBN", "1-8630-014");

     // Write the title.
     writer.WriteElementString("title", "The Handmaid's Tale");
              
     // Write the style element.
     writer.WriteStartElement("style");
     writer.WriteEntityRef("h");
     writer.WriteEndElement(); 

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

     // Write CDATA.
     writer.WriteCData("Prices 15% off!!");

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

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

}

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

XmlWriter-Klasse
XmlWriter-Member
System.Xml-Namespace

Weitere Ressourcen

Schreiben von XML mit dem "XmlWriter"