7

This is my XML file

<employee>
  <name ref="a1" type="xxx"></name>
  <name ref="a2" type="yyy"></name>
  <name ref="a3" type="zzz"></name>
</employee>

Using C#, I need to insert this node

<name ref="b2" type="aaa"></name>

between the "a2" and "a3" nodes. Any pointer how to sort this out?

1
  • Are you using XmlDocument or the Linq 2 Xml (XDocument)? Commented Jun 9, 2010 at 9:47

1 Answer 1

10

use the insertAfter method:

XmlDocument xDoc = new XmlDocument();
xDoc.Load(yourFile);
XmlNode xElt = xDoc.SelectSingleNode("//name[@ref=\"a2\"]");
XmlElement xNewChild = xDoc.CreateElement("name");
xNewChild.SetAttribute("ref", "b2");
xNewChild.SetAttribute("type", "aaa");
xDoc.DocumentElement.InsertAfter(xNewChild, xElt);
Sign up to request clarification or add additional context in comments.

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.