Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

XmlWriter.WriteComment Method

When overridden in a derived class, writes out a comment <!--...--> containing the specified text.

Namespace:  System.Xml
Assembly:  System.Xml (in System.Xml.dll)
public abstract void WriteComment(
	string text
)

Parameters

text
Type: System.String
Text to place inside the comment.
ExceptionCondition
ArgumentException

The text would result in a non-well formed XML document.

If text is either null or String.Empty, this method writes a comment with no data content, for example <!---->.

If text contains an invalid sequence of two dashes "--" the XmlWriter can either throw an ArgumentException or insert a space between the dashes "- -" so that the text is a valid XML comment (XmlWriter objects created by the Create method).

The following example navigates through the stream to determine the current node type, and then uses XmlWriter to output the XmlReader content.


StringBuilder output = new StringBuilder();

String xmlString =
        @"<?xml version='1.0'?>
        <!-- This is a sample XML document -->
        <Items>
          <Item>test with a child element <more/> stuff</Item>
        </Items>";
// Create an XmlReader
using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
{
    XmlWriterSettings ws = new XmlWriterSettings();
    ws.Indent = true;
    using (XmlWriter writer = XmlWriter.Create(output, ws))
    {

        // Parse the file and display each of the nodes.
        while (reader.Read())
        {
            switch (reader.NodeType)
            {
                case XmlNodeType.Element:
                    writer.WriteStartElement(reader.Name);
                    break;
                case XmlNodeType.Text:
                    writer.WriteString(reader.Value);
                    break;
                case XmlNodeType.XmlDeclaration:
                case XmlNodeType.ProcessingInstruction:
                    writer.WriteProcessingInstruction(reader.Name, reader.Value);
                    break;
                case XmlNodeType.Comment:
                    writer.WriteComment(reader.Value);
                    break;
                case XmlNodeType.EndElement:
                    writer.WriteFullEndElement();
                    break;
            }
        }

    }
}
OutputTextBlock.Text = output.ToString();


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.