tags:

views:

226

answers:

2

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 &lt;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.

+1  A: 

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);
    }
}
Jon Skeet
thanks, it worked fine. I tried it yesterday and didn't work. It was a long day perhaps!thanks again.
Derar
A: 

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

foson
yeah, that's another option that works for me. I wanted something with less headache. but you have to use someNode.AppendChild(doc.ImportNode(rolesNode,true ));to add it to the first DOM-tree as you are building a new DOM-tree.
Derar