在Delphi中创建OpenXMLapp.xml已添加属性
我正在.docxDelphi 中使用 OpenXML创建一个文件。当我docPropsapp.xml在 Delphi 中创建以生成 docx 时,由于某种原因总是添加一个标签。
我要创建的 XML 文件是这样的:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties"
xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">
<Template>Normal.dotm</Template>
<TotalTime>1</TotalTime>
<Pages>1</Pages>
<Words>1</Words>
...
</Properties>
通过做这个:
var
Root: IXMLNode;
Rel: IXMLNode;
Root := XMLDocument1.addChild('Properties');
... //attributes are added here
Rel := Root.AddChild('Template');
Rel.NodeValue := 'Normal.dotm';
Rel := Root.AddChild('TotalTime');
Rel.NodeValue := '1';
...
我期待上面的代码在顶部生成 XML 文件,但我得到了这个:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">
<Template xmlns="">Normal.dotm</Template >
<TotalTime xmlns=""></TotalTime>
<Pages xmlns="">1</Pages>
</Properties>
xmlns由于某种原因添加了该属性。有没有办法在顶部实现预期的 XML?
回答
创建元素时显式提供命名空间 URI:
const
PROP_NS = 'http://schemas.openxmlformats.org/officeDocument/2006/extended-properties';
var
Root: IXMLNode;
Rel: IXMLNode;
Root := XMLDocument1.AddChild('Properties', PROP_NS);
Rel := Root.AddChild('Template', PROP_NS);
Rel.NodeValue := 'Normal.dotm';
Rel := Root.AddChild('Pages', PROP_NS);
Rel.NodeValue := '1';