在Delphi中创建OpenXML app.xml增加了属性



我正在Delphi中使用OpenXML创建.docx文件。当我在Delphi中创建docPropsapp.xml以生成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';

最新更新