我正在使用XmlWriter
. 但在输出中也有版本信息。
<?xml version="1.0" encoding="utf-8"?>
我的文件中不需要这个。我怎样才能做到这一点?有没有办法通过代码删除它?
使用ConformanceLevel
和OmitXmlDeclaration
属性。例子:
XmlWriter w;
w.Settings = new XmlWriterSettings();
w.Settings.ConformanceLevel = ConformanceLevel.Fragment;
w.Settings.OmitXmlDeclaration = true;
创建 XmlWriter 时,使用 XmlWriterSettings 传递所需的设置:
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
writer = XmlWriter.Create(Console.Out, settings);
XmlWriterSettings 还具有其他属性(缩进等)。