System.Xml.XmlWriter.Create 方法 (String, XmlWriterSettings)

方法描述

使用文件名和 XmlWriterSettings 对象创建一个新的 XmlWriter 实例。

语法定义(C# System.Xml.XmlWriter.Create 方法 (String, XmlWriterSettings) 的用法)

public static XmlWriter Create(
	string outputFileName,
	XmlWriterSettings settings
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
outputFileName System-String 要写入的文件。XmlWriter 在指定路径上创建一个文件,并采用 XML 1.0 文本语法写入该文件。outputFileName 必须为文件系统路径。
settings System-Xml-XmlWriterSettings 用于配置新的 XmlWriter 实例的 XmlWriterSettings 对象。如果这是 null,则使用具有默认设置的 XmlWriterSettings。如果将 XmlWriter 用于 Transform 方法,则应使用 OutputSettings 属性获取具有正确设置的 XmlWriterSettings 对象。这样可以确保所创建的 XmlWriter 对象的输出设置是正确的。
返回值 System.Xml.XmlWriter 一个 XmlWriter 对象。

提示和注释

System.Xml.XmlWriter.Create 方法 (String, XmlWriterSettings)例子

下面的示例利用已定义的设置创建一个 XmlWriter 对象。

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

public class Sample {

  public static void Main() {

    XmlWriter writer = null;

    try {

       // Create an XmlWriterSettings object with the correct options. 
       XmlWriterSettings settings = new XmlWriterSettings();
       settings.Indent = true;
       settings.IndentChars = ("\t");
       settings.OmitXmlDeclaration = true;

       // Create the XmlWriter object and write some content.
       writer = XmlWriter.Create("data.xml", settings);
       writer.WriteStartElement("book");
       writer.WriteElementString("item", "tesing");
       writer.WriteEndElement();
	
       writer.Flush();

     } 
     finally  {
        if (writer != null)
          writer.Close();
     }
  } 
}

异常

异常 异常描述
ArgumentNullException url 值为 null。

命名空间

namespace: System.Xml

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

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.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 系统要求。