I have a xml which is having multiple siteserver element i want to group all the siteserver element to Siteservers parent node

I have a xml which is having multiple siteserver element i want to group all the siteserver element to Siteservers parent node



Currently xml is getting generated like below


<Sites>
<SiteServer>
<ID>4</ID>
<SiteID>4</SiteID>
<ServerName>New server</ServerName>
<IPAddress>1022216522</IPAddress>
<PDPServer>true</PDPServer>
<bFTPS>false</bFTPS>
</SiteServer>
<SiteServer>
<ID>4</ID>
<SiteID>4</SiteID>
<ServerName>New server</ServerName>
<IPAddress>1022216522</IPAddress>
<PDPServer>true</PDPServer>
<bFTPS>false</bFTPS>
</SiteServer>
</Sites>



I need to add a parent node before siteserver name siteservers. group all the siteserver to siteservers.


<Sites>
<SiteServers>
<SiteServer>
<ID>4</ID>
<SiteID>4</SiteID>
<ServerName>New server</ServerName>
<IPAddress>1022216522</IPAddress>
<PDPServer>true</PDPServer>
<bFTPS>false</bFTPS>
</SiteServer>
<SiteServer>
<ID>4</ID>
<SiteID>4</SiteID>
<ServerName>New server</ServerName>
<IPAddress>1022216522</IPAddress>
<PDPServer>true</PDPServer>
<bFTPS>false</bFTPS>
</SiteServer>
</SiteServers>
</Sites>



i have tried adding child node and group by but didnot work.


var xml = XDocument.Load(@"C:UsersrbarnwalsourcerepostestXMLtestXMLExportXml2.xml");
var result = new XElement(xml.Root.Name, xml.Root.Elements().GroupBy(x => x.Name).Select(x => new XElement("Parent", new XAttribute("name", x.Key))));

var result2 = new XElement(xml.Root.Name, xml.Root.Attributes().ToArray(),
xml.Root.Elements().GroupBy(x => x.Element("SiteServer").Value.Trim()).Select(x =>
new XElement("SiteServers"))
);




2 Answers
2



Hope this will help. From my XML journies I am used to build xml files from inside out.


var doc = new XDocument();
var root = new XElement("Sites"); // Sites node
var siteServers = new XElement("SiteServers"); // SiteServers node
List<XElement> siteServerList = new List<XElement>(); // list of SiteServer nodes
//I am not sure, where you are getting ID, siteID etc., so I will pretend like you are adding them by foreach, like from list, or something.
foreach( var site in sites)

XElement siteServer = new XElement("SiteServer");
siteServer.Add(new XElement("ID", site.ID));
siteServer.Add(new XElement("SiteID", site.siteID));
// etc...

siteServerList.Add(siteServer);


foreach(var siteServer in siteServerList)

siteServers.Add(siteServer);

root.Add(siteServers);
doc.Add(root);
doc.Save("YourPath");



Not simple. Try code below :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;


namespace ConsoleApplication1

class Program

const string FILENAME = @"c:temptest.xml";
static void Main(string args)

XDocument doc = XDocument.Load(FILENAME);
XElement sites = doc.Descendants("Sites").FirstOrDefault();

var groups = doc.Descendants("SiteServer").GroupBy(x => (int)x.Element("ID")).ToList();

XElement newSites = new XElement("Sites");
foreach(var group in groups)

XElement newSiteServers = new XElement("SiteServers", group);
newSites.Add(newSiteServers);

sites.ReplaceWith(newSites);








Thanks for your answer but both the aproach is not working. In second sitesserver is getting added for every site server when it has to be added only once.

– Ritanshu Barnwal
Sep 17 '18 at 12:00






Can you give better example of the Original XML so I can test code. From you request it looked like you wanted the servers grouped so that is what I did. You didn't give an example with multiple IDs. Your request said "i want to group all the siteserver element to Siteservers parent node"

– jdweng
Sep 17 '18 at 16:52




Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế