【问题标题】:innerText of XmlNode from a String来自字符串的 XmlNode 的 innerText
【发布时间】:2009-09-03 00:54:06
【问题描述】:

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

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

【问题讨论】:

    标签: c# xml xmlnode


    【解决方案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);
        }
    }
    

    【讨论】:

    • 谢谢,它运行良好。我昨天试过了,没有用。这也许是漫长的一天!再次感谢。
    【解决方案2】:

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

    【讨论】:

    • 是的,这是另一个适合我的选择。我想要一些不那么头痛的东西。但你必须使用 someNode.AppendChild(doc.ImportNode(rolesNode,true ));在构建新的 DOM 树时将其添加到第一个 DOM 树。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多