tags:

views:

214

answers:

2

Hello, this may have been asked before, but I could not find it.

Suppose I have an XML element

XMLElement nd = xmlDoc.CreateElement("Node");

Now, I would like to add a child to nd with a full XML snippet I get from some other function, like this:

nd.AppendChild("<a1><a2></a2></a1>");

What is the best way to do this?

Thanks.

+4  A: 
nb.InnerXML = "<a1><a2></a2></a1>";
qux
A: 

The way above is not the "Best" way to do this. The elements are nodes and should be created and added in that fashion. (Not real code, close).

XMLElement nd = xmlDoc.CreateElement("Node");
XMLElement a1 = xmlDoc.CreateElement("Node");
XMLElement a2 = xmlDoc.CreateElement("Node");

//Add the node name etc.

nd.AppendChild(a1);
nd.AppendChild(a2);

It is not good to use "<a1>" strings. What if the namespace changes? What about special characters, don't want to process them yourself, right?

Ted Johnson