我正在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';