我有一个具有多个Siteserver元素的xml,我想将所有siteserver元素分组到Siteservers父节点

时间:2018-09-17 11:02:07

标签: c# xml linq

目前正在生成xml,如下所示

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

我需要在站点服务器名称“站点服务器”之前添加一个父节点。将所有站点服务器分组到站点服务器。

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

我尝试添加子节点和分组依据,但是没有用。

var xml = XDocument.Load(@"C:\Users\rbarnwal\source\repos\testXML\testXML\ExportXml2.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 个答案:

答案 0 :(得分:0)

希望这会有所帮助。从我的XML旅程中,我习惯了从内而外构建xml文件。

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

答案 1 :(得分:0)

不简单。尝试以下代码:

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:\temp\test.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);
        }
    }
}