这种方法有什么作用,为什么有必要?
“转到定义”选项将我带到一个包含所有注释的函数,这是它关于其目的的说明
//Writes the XML declaration with the version "1.0" and the standalone attribute.
写 XML 声明是什么意思?
这是它创建.xml
文件的时候吗?
它与构造函数 XmlTextWriter() 的用途有何关系?
这种方法有什么作用,为什么有必要?
“转到定义”选项将我带到一个包含所有注释的函数,这是它关于其目的的说明
//Writes the XML declaration with the version "1.0" and the standalone attribute.
写 XML 声明是什么意思?
这是它创建.xml
文件的时候吗?
它与构造函数 XmlTextWriter() 的用途有何关系?
首先,它属于XmlWriter
,不是真的XmlTextWriter
。正如 John Saunders 所说,创建 an 的首选方式XmlWriter
是 via XmlWriter.Create
。
的重点WriteStartDocument
是在输出流中创建它:
<?xml version="1.0" ?>
这不是在您创建XmlWriter
. 它还可能指定编码。(如果未指定编码,XML 默认为 UTF-8 或 UTF-16。)
至于您是否需要它 - XML 文档不必有XML 声明,但它们“应该”根据规范(即这是最佳实践)。
XML 声明是这样的:
<?xml version="1.0" encoding="utf-8"?>
它应该位于每个 XML 文档之上,因为它会告知解析器所使用的编码(以及 XML 版本,但在今天这并不重要)。
所以
writer = XmlWriter.Create("file path");
具有相同的效果
XmlTextWriter writer = new XmlTextWriter("file path", type);
正确的?
回复了,你们好快啊!
是否有任何理由使用 Xmltextwriter,或者它只是过时了?我只是问,因为我看到很多使用 XmlTextWriter 和 XmlTextReader 的资源。