我正在用Delphi中的.docx创建一个OpenXML文件。当我在Delphi中创建\docProps\app.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?
发布于 2021-08-20 07:51:34
在创建元素时显式提供命名空间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';https://stackoverflow.com/questions/68856644
复制相似问题