如何在Delphi 7中使用TXML Document创建XML文件

5
我已经使用以下代码创建XML文档:

procedure TForm1.btnCreateXMLClick(Sender: TObject);
  var
   rootName:string;
  childName:string;
  attrChild:string;
   iXml: IDOMDocument;
  iRoot, iNode, iNode2, iChild, iAttribute: IDOMNode;
begin
  XMLDoc.Active:=false;
   XMLDoc.XML.Text:='';
   XMLDoc.Active:=true;
    XMLDoc.FileName:='C:\Documents and Settings\a\Desktop\New Text Document.xml';
     iXml := XmlDoc.DOMDocument;
   //iRoot:=iXml.documentElement(iXml.createElement('xml'));
    iRoot := iXml.appendChild(iXml.createElement ('xml'));
     // node "test"
     iNode := iRoot.appendChild (iXml.createElement ('test'));
      iNode.appendChild (iXml.createElement ('test2'));
   iChild := iNode.appendChild (iXml.createElement ('test3'));
    iChild.appendChild (iXml.createTextNode('simple value'));
     iNode.insertBefore (iXml.createElement ('test4'), iChild);

   // node replication
     iNode2 := iNode.cloneNode (True);
  iRoot.appendChild (iNode2);

    // add an attribute
      iAttribute := iXml.createAttribute ('color');
        iAttribute.nodeValue := 'red';
      iNode2.attributes.setNamedItem (iAttribute);

    // show XML in memo
      memXMLOutput.Lines.Text:=FormatXMLData(XMLDoc.XML.Text);
   end;

我在memXMLOutput中得到了输出,但是当在记事本或IE中查看XML文档时,输出并未显示。问题出在哪里?提前感谢。

4
恭喜您在 Delphi 标签中发布了第一万个问题。 - Andreas Rejbrand
哎呀!第一万个 Delphi 问题加一分! - Cosmin Prund
YIPEE!!!! 我是世界的大师 - CyprUS
下次应该在 http://stackoverflow.com/questions/6666666/ 发布,以避免不明显的获取。 - Premature Optimization
1个回答

6

删除此内容:

XMLDoc.FileName:='C:\Documents and Settings\a\Desktop\New Text Document.xml';

在代码完成创建 XML 文档后,可以添加类似以下内容的代码:
XMLDoc.SaveToFile('C:\Documents and Settings\a\Desktop\New Text Document.xml');

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接