1

I have the following XML code

<person>
  <name>prane</name>
  <age>19</age>
</person>
<person>
  <name>neeth</name>
  <age>20</age>
</person>

Now I want to add parent node using c# the resulting XML should be

<persons>
   <person>
      <name>prane</name>
      <age>19</age>
   </person>
   <person>
      <name>neeth</name>
      <age>20</age>
   </person>
</persons>

How can it be done?

3
  • It would help to see what you currently using to manipulate XML so appropriate recommendations can be made. There are couple existing questions about adding nodes to XML (you can find them by bing.com/search?q=C%23+add+node+xml) - see if any of the questions help and possibly let you add more specific details to your post. Commented Jul 9, 2015 at 2:43
  • Have you taken a look at the documentation or wiki for the XML library you are using? Or even just googling [xml library] insert parent?? Commented Jul 9, 2015 at 2:50
  • Your first example isn't XML as it has multiple root nodes. So, given this you are really working with strings and not XML. This then becomes plain string concatenation. Commented Jul 9, 2015 at 3:32

1 Answer 1

1

Your code can not be XML,it just looks like XML,because it does have root elements,if the xml you said just a string,you can do like this

string xml = @"<person>
  <name>prane</name>
  <age>19</age>
</person>
<person>
  <name>neeth</name>
  <age>20</age>
</person>";
            XElement root = XElement.Parse(string.Format("{0}{1}{2}", "<persons>", xml, "</persons>"));
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.