XmlComment Class

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

Represents a comment node in the XML document.

Inheritance

Syntax

public class XmlComment : XmlCharacterData

Remarks

An XmlComment node is used to store comments in an XML document. Comments are ignored by the XML parser but can be read by applications. This class provides methods to create and manipulate comment nodes within an XmlDocument.

Constructors

XmlComment(string data, XmlDocument doc)

public XmlComment (string data, XmlDocument doc);

Initializes a new instance of the XmlComment class.

data
The content of the comment. This value is typically a string.
doc
The XmlDocument to which the comment will be added.

Properties

Name Description
BaseURI Gets the base URI of the current node.
ChildNodes Gets a XmlNodeList containing the children of this node. For XmlComment, this will always be an empty list.
Data Gets or sets the value of this node. For an XmlComment, this is the text content of the comment.
DocumentType Gets the XmlDocument to which this node belongs.
FirstChild Gets the first child of this node. For XmlComment, this is always null.
InnerXml Gets or sets a value indicating whether the node is read-only. XmlComment nodes are generally mutable unless the document itself is read-only.
IsReadOnly Gets a value indicating whether the node is read-only. XmlComment nodes are generally mutable unless the document itself is read-only.
LastChild Gets the last child of this node. For XmlComment, this is always null.
LocalName Gets the local name of the node. For XmlComment, this is always #comment.
Name Gets the qualified name of the node. For XmlComment, this is always #comment.
NextSibling Gets the node immediately following this node.
NodeType Gets the type of the current node. Returns XmlNodeType.Comment.
OuterXml Gets the markup representing this node and all its children. For an XmlComment, this will be the comment markup (e.g., `<!-- My comment -->`).
OwnerDocument Gets the XmlDocument associated with this node.
ParentNode Gets the parent of this node (XmlDocument or another XmlNode).
Prefix Gets the namespace prefix of this node. For XmlComment, this is always an empty string.
PreviousSibling Gets the node immediately preceding this node.
Value Gets or sets the text content of the comment.

Methods

Name Description
CloneNode(bool deep) Creates a copy of this node. If deep is true, also copies all the children of the node. For XmlComment, the deep parameter has no effect as comments have no children.
AppendChild(XmlNode newChild) Appends the specified node to the end of the list of children of this node. This method throws an exception for XmlComment as it cannot have children.
InsertAfter(XmlNode newChild, XmlNode refChild) Inserts the specified node after the specified reference node. This method throws an exception for XmlComment as it cannot have children.
InsertBefore(XmlNode newChild, XmlNode refChild) Inserts the specified node before the specified reference node. This method throws an exception for XmlComment as it cannot have children.
Normalize() Ensures that all the nodes in the subtree are in a "normal" form, that is, that there are no empty text nodes and that the nodes are maximally merged. For XmlComment, this operation typically has no visible effect.
RemoveAll() Removes all the children of this node. This method throws an exception for XmlComment as it cannot have children.
RemoveChild(XmlNode oldChild) Removes the specified child node. This method throws an exception for XmlComment as it cannot have children.
ReplaceChild(XmlNode newChild, XmlNode oldChild) Replaces the child node oldChild with newChild. This method throws an exception for XmlComment as it cannot have children.
WriteTo(XmlWriter w) Saves the current node to the specified XmlWriter.

Example

The following C# example demonstrates how to create an XmlDocument, add a comment to it, and then write the document to the console.

using System;
using System.Xml;

public class Example
{
    public static void Main(string[] args)
    {
        // Create a new XmlDocument
        XmlDocument doc = new XmlDocument();

        // Create an XML declaration node
        XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", "utf-8", null);
        doc.AppendChild(xmlDeclaration);

        // Create a root element
        XmlElement root = doc.CreateElement("root");
        doc.AppendChild(root);

        // Create an XmlComment node
        XmlComment comment = doc.CreateComment("This is a sample comment.");
        root.AppendChild(comment);

        // Create another element
        XmlElement element = doc.CreateElement("item");
        element.InnerText = "Some data";
        root.AppendChild(element);

        // Save the document to the console
        Console.WriteLine(doc.OuterXml);
    }
}

Output:

<?xml version="1.0" encoding="utf-8"?>
<root><!--This is a sample comment.--><item>Some data</item></root>