c# - Linq XML add new parent -


with linq xml possible add new father existing nodes? take xml excerpt:

<?xml version="1.0" encoding="utf-8" standalone="yes"?> <items>  <book>   <title>title 1</title>   <author>author 1</author>  </book>  <book>   <title>title 2</title>   <author>author 2</author>  </book>  <car>   <model>tesla</model>  </car> </items> 

is possible add new father "books" book this:

<?xml version="1.0" encoding="utf-8" standalone="yes"?> <items>  <books>   <book>    <title>title 1</title>    <author>author 1</author>   </book>   <book>    <title>title 2</title>    <author>author 2</author>   </book>  </books>   <car>    <model>tesla</model>   </car> </items> 

this not working because cloning nodes:

doc.element("items").add(new xelement("books",doc.element("items").elements("book"))); 

you can remove existed <book> elements <items> node after adding them under new <books> parent node:

var books = doc.element("items").elements("book"); doc.element("items").add(new xelement("books", books)); books.remove(); 

Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - How to Hide Date Menu from Datepicker in yii2 -