2009-09-02 48 views
0

反正我可以用字符串創建節點嗎?我在網上搜索了一些東西,但找不到任何可行的東西!字符串中的XmlNode的innerText

string _configFileName = @"d:\junk\config.xml"; 
XmlDocument xmldoc = new XmlDocument(); 
xmldoc.Load(_configFileName); 

string xmlTags = @"<queue name=queueName autoStart=true> 
    <deleteFile>true</deleteFile> 
    <impersonation enabled=true> 
    <user>domain\username</user> 
    <password encrypted="true">********</password> 
    </impersonation> 
    <tasks> 
    <task>cp</task> 
    <task>rm</task> 
    </tasks> 
    </queue>"; 
    queueParent.InnerText = str;//the Xml parent node of the new queue node that I want to add 
    xmldoc.Save();//will write &lt;queue name= INSTEAD OF <queue name= 

所以問題是具有XML「<」特殊字符和「>」寫入文件爲「<」和「>」。 您的意見非常感謝,謝謝。

回答

1

我想你想要InnerXml財產,而不是InnerText

例如:

using System; 
using System.Xml; 

class Test 
{ 
    static void Main() 
    { 
     XmlDocument doc = new XmlDocument(); 
     XmlElement root = doc.CreateElement("root"); 
     doc.AppendChild(root); 
     root.InnerXml = "<child>Hi!</child>"; 
     doc.Save(Console.Out); 
    } 
} 
+0

感謝串一個XmlDocument,它工作得很好。我昨天試過了,並沒有工作。也許是漫長的一天! 再次感謝。 – Derar 2009-09-02 17:19:25

0

您可以創建使用xmldoc.LoadXml(xmlTags)

+0

是的,這是另一種適用於我的選項。我想要的東西少一點頭痛。但你必須使用 someNode.AppendChild(doc.ImportNode(rolesNode,true)); 將它添加到第一個DOM樹,因爲您正在構建一個新的DOM樹。 – Derar 2009-09-02 17:23:09