0

无论如何我可以用字符串创建节点吗?我在网上搜索了一些东西,但找不到任何有用的东西!

 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“<”和“>”中的特殊字符作为“<”和“>”写入文件。非常感谢您的意见,谢谢。

4

2 回答 2

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);
    }
}
于 2009-09-02T16:53:39.720 回答
0

您可以使用 xmldoc.LoadXml(xmlTags) 从字符串创建 XmlDocument

于 2009-09-02T16:54:06.100 回答