2011-07-08 40 views
5

我用下面的代碼來創建XML文檔:如何在Delphi中創建一個使用TXML文檔的XML文件7

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但XML文檔不顯示輸出時在IE的記事本中看到。哪裏有問題?在此先感謝

+4

恭喜你,你已經張貼在Delphi萬分之一問題標籤。 –

+0

唉! +1第一萬德爾福問題! –

+0

YIPEE !!!!我是世界的主人 – CyprUS

回答

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'); 
+0

感謝Cosmin。有效 – CyprUS