c# - Grouping particular nodes to their parent nodes inside XML -


below given sample xml:

<response>     <transactions>         <upload>             <documentid>1</documentid>             <name>a</name>             <documentid>2</documentid>             <name>b</name>         </upload>         <upload>             <documentid>3</documentid>             <name>c</name>             <documentid>4</documentid>             <name>d</name>         </upload>         <upload>             <documentid>5</documentid>             <name>e</name>             <documentid>6</documentid>             <name>f</name>         </upload>     <transactions> </response> 

below given coming output:

<total>     <documentid>1</documentid>     <documentid>2</documentid>     <documentid>3</documentid>     <documentid>4</documentid>     <documentid>5</documentid>     <documentid>6</documentid> </total> 

i using following given code getting output:

var downloadfolder = @"c:\users\kumarmohanta\desktop\new folder\test.xml";  var masterfile = new xdocument(); xelement newdocument = new xelement("total"); masterfile.add(newdocument);  xdocument xdoc = xdocument.load(downloadfolder);  masterfile.root.add(xdoc.descendants("documentid")); masterfile.save(@"c:\users\kumarmohanta\desktop\merged_.xml"); 

but desired output should this:

<total>     <transactions>         <upload>             <documentid>1</documentid>             <documentid>2</documentid>         </upload>         <upload>             <documentid>3</documentid>             <documentid>4</documentid>         </upload>         <upload>             <documentid>5</documentid>             <documentid>6</documentid>         </upload>     <transactions> </total> 

rename element transactions total. remove elements name name. save file.

try this:

var downloadfolder = @"c:\users\kumarmohanta\desktop\new folder\test.xml";  xdocument xdoc = xdocument.load(downloadfolder); xdoc.root.name = "total"; xdoc.descendants("upload").elements("name").remove();  xdoc.save(@"c:\users\kumarmohanta\desktop\merged_.xml"); 

note xml example not close transactions element.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -