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