如何在XML文档中创建xmlns:xsi和xsd信息

前端之家收集整理的这篇文章主要介绍了如何在XML文档中创建xmlns:xsi和xsd信息前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个Delphi XE应用程序,它读取经过验证的 XML文件,修改它然后保存它.可以验证保存的版本.我使用SML Spy来创建文件并对其进行验证.

现在我需要在内存中创建一个文档并保存它.问题是我无法弄清楚如何为文档生成xmlns和xsd信息属性,以便可以验证它.

解决方法

实际上,尽管我上面的评论,我发现最简单的方法不是使用DeclareNamespace.

这是一个甚至在表单上都不使用TXMLDocument的示例.只需将xmldom,XMLIntf和XMLDoc添加到您的implementation uses子句(Xml的Xml.xmldom,Xml.XMLIntf和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相关文章