12

我正在使用XmlWriter. 但在输出中也有版本信息。

<?xml version="1.0" encoding="utf-8"?>

我的文件中不需要这个。我怎样才能做到这一点?有没有办法通过代码删除它?

4

3 回答 3

19

使用ConformanceLevelOmitXmlDeclaration属性。例子:

XmlWriter w;
w.Settings = new XmlWriterSettings();
w.Settings.ConformanceLevel = ConformanceLevel.Fragment;
w.Settings.OmitXmlDeclaration = true;
于 2009-12-30T13:28:20.653 回答
12

创建 XmlWriter 时,使用 XmlWriterSettings 传递所需的设置:

XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;

writer = XmlWriter.Create(Console.Out, settings);

XmlWriterSettings 还具有其他属性(缩进等)。

于 2009-12-30T13:28:32.413 回答
0

您可以使用

XmlWriterSettings 类

并使用XmlWriterSettings.OmitXmlDeclaration 属性

于 2009-12-30T13:28:20.323 回答