我有一个 Delphi XE 应用程序,它读取经过验证的 XML 文件,修改它,然后保存它。可以验证保存的版本。我使用 SML Spy 创建文件并验证它。
现在我需要在内存中创建一个文档并保存它。问题是我无法弄清楚如何为文档生成 xmlns 和 xsd 信息属性,以便对其进行验证。
最佳答案
实际上,尽管我在上面发表了评论,但我发现最简单的方法不是使用 DeclareNamespace
。
这是一个甚至不在表单上使用 TXMLDocument
的示例。只需将 xmldom
、XMLIntf
和 XMLDoc
添加到您的实现 uses 子句 (Xml.xmldom
, Xml .XMLIntf
,以及 XE2 的 Xml.XMLDoc
),然后就可以了:
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"/>
关于xml - 如何创建xmlns :xsi and xsd information in an XML document,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11075366/