我有一个 Delphi XE 应用程序,它读取经过验证的 XML 文件,对其进行修改然后保存。可以验证保存的版本。我使用 SML Spy 创建文件并对其进行验证。
现在我需要在内存中创建一个文档并保存它。问题是我无法弄清楚如何为文档生成 xmlns 和 xsd 信息属性,以便对其进行验证。
实际上,尽管我在上面发表了评论,但我发现最简单的方法不是使用DeclareNamespace
.
这是一个甚至没有TXMLDocument
在表单上使用 a 的示例。只需将xmldom
, XMLIntf
, and添加XMLDoc
到您的实现中使用子句(Xml.xmldom
, Xml.XMLIntf
, and Xml.XMLDoc
for XE2),然后就可以了:
procedure TForm1.Button1Click(Sender: TObject);
var
TheDoc: IXmlDocument;
iNode: IXmlNode;
xmlText: DOMString;
begin
TheDoc := NewXMLDocument;
TheDoc.Version := '1.0';
TheDoc.Encoding := 'UTF-16';
iNode := TheDoc.AddChild('test:test_file');
iNode.SetAttributeNS('xmlns:test', '', 'http://www.foo.com' );
iNode.SetAttributeNS('xmlns:xsi', '', 'http://www.w3.org/2001/XMLSchema');
TheDoc.SaveToXML(xmlText);
Memo1.Lines.Text := xmlText;
end;
以上结果在以下输出中TMemo
:
<?xml version="1.0" encoding="UTF-16"?>
<test:test_file xmlns:test="http://www.foo.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema"/>