简体   繁体   中英

How to add parent node in XML using c#

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?

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>"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM