简体   繁体   English

为什么 MSXML 中的 createProcessingInstruction 生成不完整的输出?

[英]Why does createProcessingInstruction in MSXML generate incomplete output?

The following VBA code generates <?xml version="1.0"?> as output.以下 VBA 代码生成<?xml version="1.0"?>作为输出。

Dim XML As New DomDocument 
Dim pi As IXMLDOMProcessingInstruction

'.... some code that sets the root element of the document

Set pi = XML.createProcessingInstruction("xml", "version='1.0' encoding='UTF-8'")
        XML.insertBefore pi, XML.documentElement

Why is the encoding="UTF-8" left out?为什么encoding="UTF-8"遗漏了?

<?xml?> is not a Processing Instruction. <?xml?>不是处理指令。 The <?xml?> construct is called the XML declaration and obeys different rules to a real Processing Instruction. <?xml?> 构造称为XML 声明,它遵循与实际处理指令不同的规则。 Creating a PI with name 'xml' is not well-formed;创建名称为“xml”的 PI 格式不正确; names beginning with 'xml' are reserved .以 'xml' 开头的名称是保留的

To change the format of the prolog you need to configure an XMLWriter , setting 'version', 'encoding' or simply 'omitXMLDeclaration' to remove it altogether, which is perfectly fine since 1.0 and UTF-8 are the default values.要更改序言的格式,您需要配置一个 XMLWriter ,设置 'version'、'encoding' 或简单的 'omitXMLDeclaration' 以将其完全删除,这完全没问题,因为 1.0 和 UTF-8 是默认值。

UTF-8 is the default encoding anyway.无论如何,UTF-8 是默认编码。 What you've specified is redundant, so the serializer likely omits it.您指定的内容是多余的,因此序列化程序可能会忽略它。 The version field is not an optional field, though.但是, version字段不是可选字段。 If the file has an XML declaration at all, the declaration must include the version field .如果文件有 XML 声明,则声明必须包含version字段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM