来自字符串的 XmlNode 的内部文本

发布于 2024-08-04 11:19:09 字数 835 浏览 2 评论 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 中的特殊字符“<”和“>”以“<”形式写入文件和“>”。 非常感谢您的意见,谢谢。

Is there anyway I can create nodes out of a string? I've searched the Web looking for something, but couldn't find anything that works!

 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 <queue name= INSTEAD OF <queue name=

So the problem is having the special characters in XML "<" and ">" written into the file as "<" and ">".
Your input is much appreciated, thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

dawn曙光 2024-08-11 11:19:09

我认为您想要 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);
    }
}

I think you want the InnerXml property instead of InnerText.

For example:

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);
    }
}
忆伤 2024-08-11 11:19:09

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

You can create an XmlDocument from a string using xmldoc.LoadXml(xmlTags)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文