public abstract WriteComment ( string text ) : void | ||
text | string | |
return | void |
XmlWriter writer = XmlWriter.Create("output.xml"); writer.WriteStartDocument(); writer.WriteComment("This is a comment"); // writes "" writer.WriteEndDocument(); writer.Close();
XmlDocument doc = new XmlDocument(); XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.OmitXmlDeclaration = true; settings.NewLineOnAttributes = true; using (XmlWriter writer = XmlWriter.Create(Console.Out, settings)) { writer.WriteStartElement("root"); writer.WriteComment("This is a comment"); writer.WriteEndElement(); }In this example, an XmlWriter is created to write to the console. The XmlWriterSettings object is used to specify that the output should be indented, the XML declaration should not be written, and new lines should be added after attributes. The WriteStartElement method writes the beginning of an XML element, followed by a comment using the WriteComment method, and then the WriteEndElement method writes the end of an XML element. Package library: System.Xml.