0

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?

2 Answers 2

3
nb.InnerXML = "<a1><a2></a2></a1>";
Sign up to request clarification or add additional context in comments.

Comments

0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.