System.Xml.XmlDocument.CreateComment 方法

方法描述

创建包含指定数据的 XmlComment。

语法定义(C# System.Xml.XmlDocument.CreateComment 方法 的用法)

public virtual XmlComment CreateComment(
	string data
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
data System-String 新 XmlComment 的内容。
返回值 System.Xml.XmlComment 新的 XmlComment。

提示和注释

尽管此方法在文档的上下文中创建新对象,但它并不自动将新对象添加到文档树。 若要添加新对象,必须显式调用节点插入方法之一。

根据 W3C 可扩展标记语言 (XML) 1.0 建议 (www.w3.org/TR/1998/REC-xml-19980210),Comment 节点只能包含在 Document、Element 和 EntityReference 节点(当 EntityReference 节点不是 Attribute 节点的子级时)中。

System.Xml.XmlDocument.CreateComment 方法例子

下面的示例创建一个注释并将其添加到 XML 文档中。

using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("" +
                "Pride And Prejudice" +
                "");

    //Create a comment.
    XmlComment newComment;
    newComment = doc.CreateComment("Sample XML document");

    //Add the new node to the document.
    XmlElement root = doc.DocumentElement;
    doc.InsertBefore(newComment, root);

    Console.WriteLine("Display the modified XML...");        
    doc.Save(Console.Out);

  }
}

异常

异常 异常描述

命名空间

namespace: System.Xml

程序集: System.Xml(在 System.Xml.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。