如何从 C# 中的数据表生成 XML
How to generate XML from datatable in c#
我有一个来自数据库的数据表,其中包含如下数据
ID NAME SERVICE SITENAME NODENAME
27883481 EAGLE HILLS PROPERTIES MANAGED WAN CAPITAL GATE-ADNEC LEANING TOWER capitalgatetwrill-ra
27883481 EAGLE HILLS PROPERTIES MANAGED WAN 2020 BLDG dxbcontactcentreill-rb
27883481 EAGLE HILLS PROPERTIES MANAGED WAN 2020 BLDG dxbcontactcentreill-ra
27883481 EAGLE HILLS PROPERTIES MANAGED WAN CAPITAL GATE-ADNEC LEANING TOWER capitalgatetwrill-rb
27883481 EAGLE HILLS PROPERTIES MANAGED LAN CAPITAL GATE-ADNEC LEANING TOWER capitalgatetwrill-ra
27883481 EAGLE HILLS PROPERTIES MANAGED LAN 2020 BLDG dxbcontactcentreill-rb
27883481 EAGLE HILLS PROPERTIES MANAGED LAN 2020 BLDG dxbcontactcentreill-ra
27883481 EAGLE HILLS PROPERTIES MANAGED LAN CAPITAL GATE-ADNEC LEANING TOWER capitalgatetwrill-rb
27883 EAGLE DRILLS PROPERTIES MANAGED WAN CAPITAL GATE-ADNEC LEANING TOWER capitalgatetwrill-ra
27883 EAGLE DRILLS PROPERTIES MANAGED WAN 2020 BLDG dxbcontactcentreill-rb
27883 EAGLE DRILLS PROPERTIES MANAGED WAN 2020 BLDG dxbcontactcentreill-ra
27883 EAGLE DRILLS PROPERTIES MANAGED WAN CAPITAL GATE-ADNEC LEANING TOWER capitalgatetwrill-rb
我想根据上面的数据创建 XMl,如下所示,这是 XML
的原型
<?xml version="1.0" encoding="utf-8" ?>
<AllSites>
<SITE_NODES>
<ID>27883481</ID>
<NAME>EAGLE HILLS PROPERTIES""</NAME>
<PARTY_SERVICES>
<SERVICE_NAME>MANAGED WAN</SERVICE_NAME>
<SERVICE_SITES>
<SITE_NAME>CAPITAL GATE-ADNEC LEANING TOWER</SITE_NAME>
<SITE_NODES>
<NODE_NAME>capitalgatetwrill-ra</NODE_NAME>
<NODE_NAME>capitalgatetwrill-rb</NODE_NAME>
</SITE_NODES>
</SERVICE_SITES>
<SERVICE_SITES>
<SITE_NAME>2020 BLDG</SITE_NAME>
<SITE_NODES>
<NODE_NAME>dxbcontactcentreill-rb</NODE_NAME>
<NODE_NAME>dxbcontactcentreill-ra</NODE_NAME>
</SITE_NODES>
</SERVICE_SITES>
</PARTY_SERVICES>
<PARTY_SERVICES>
<SERVICE_NAME>MANAGED LAN</SERVICE_NAME>
<SERVICE_SITES>
<SITE_NAME>CAPITAL GATE-ADNEC LEANING TOWER</SITE_NAME>
<SITE_NODES>
<NODE_NAME>abcd-ra</NODE_NAME>
<NODE_NAME>abcd-rb</NODE_NAME>
</SITE_NODES>
</SERVICE_SITES>
<SERVICE_SITES>
<SITE_NAME>2021 BLDG</SITE_NAME>
<SITE_NODES>
<NODE_NAME>pqms-rb</NODE_NAME>
<NODE_NAME>pqms-ra</NODE_NAME>
</SITE_NODES>
</SERVICE_SITES>
</PARTY_SERVICES>
</SITE_NODES>
</AllSites>
如何生成它?我是否需要创建一个带有占位符的 XML 模板,应该在运行时替换它?或者创建 c# 类 并生成它
如下
public class DetailsBO
{
public string ID { get; set; }
public string NAME { get; set; }
public string SERVICE { get; set; }
public List<SitesBO> sites { get; set; }
}
public class SitesBO
{
public string SITENAME { get; set; }
public List<NodesBO> noodes { get; set; }
}
public class NodesBO
{
public string NODENAME { get; set; }
}
谁能给我c#代码
编辑
假设如果站点名称为空或为空,则所有站点都将成为与 WAN 或 LAN 相同的服务类型
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(string));
dt.Columns.Add("NAME", typeof(string));
dt.Columns.Add("SERVICE", typeof(string));
dt.Columns.Add("SITENAME", typeof(string));
dt.Columns.Add("NODENAME", typeof(string));
dt.Rows.Add(new object[] { "27883481", "EAGLE HILLS PROPERTIES", "MANAGED WAN", "", "capitalgatetwrill-ra" });
dt.Rows.Add(new object[] { "27883481", "EAGLE HILLS PROPERTIES", "MANAGED WAN", "2020 BLDG", "dxbcontactcentreill-rb" });
dt.Rows.Add(new object[] { "27883481", "EAGLE HILLS PROPERTIES", "MANAGED WAN", "2020 BLDG", "dxbcontactcentreill-ra" });
dt.Rows.Add(new object[] { "27883481", "EAGLE HILLS PROPERTIES", "MANAGED WAN", "CAPITAL GATE-ADNEC LEANING TOWER", "capitalgatetwrill-rb" });
dt.Rows.Add(new object[] { "27883481", "EAGLE HILLS PROPERTIES", "MANAGED LAN", "CAPITAL GATE-ADNEC LEANING TOWER", "capitalgatetwrill-ra" });
dt.Rows.Add(new object[] { "27883481", "EAGLE HILLS PROPERTIES", "MANAGED LAN", "2020 BLDG", "dxbcontactcentreill-rb" });
dt.Rows.Add(new object[] { "27883481", "EAGLE HILLS PROPERTIES", "MANAGED LAN", "2020 BLDG", "dxbcontactcentreill-ra" });
dt.Rows.Add(new object[] { "27883481", "EAGLE HILLS PROPERTIES", "MANAGED LAN", "CAPITAL GATE-ADNEC LEANING TOWER", "capitalgatetwrill-rb" });
dt.Rows.Add(new object[] { "27883", "EAGLE DRILLS PROPERTIES", "MANAGED WAN", "CAPITAL GATE-ADNEC LEANING TOWER", "capitalgatetwrill-ra" });
dt.Rows.Add(new object[] { "27883", "EAGLE DRILLS PROPERTIES", "MANAGED WAN", "2020 BLDG", "dxbcontactcentreill-rb" });
dt.Rows.Add(new object[] { "27883", "EAGLE DRILLS PROPERTIES", "MANAGED WAN", "2020 BLDG", "dxbcontactcentreill-ra" });
dt.Rows.Add(new object[] { "27883", "EAGLE DRILLS PROPERTIES", "MANAGED WAN", "CAPITAL GATE-ADNEC LEANING TOWER", "capitalgatetwrill-rb" });
最简单的代码就是取当前数据table直接输出到xml。您可以修改代码以使用将创建 类 并序列化 类 的实体。但是因为你已经有了数据table,所以我直接拿了table然后xml。看起来您删除了重复项,所以我也这样做了。结果我也下单了
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(string));
dt.Columns.Add("NAME", typeof(string));
dt.Columns.Add("SERVICE", typeof(string));
dt.Columns.Add("SITENAME", typeof(string));
dt.Columns.Add("NODENAME", typeof(string));
dt.Rows.Add(new object[] { "27883481", "EAGLE HILLS PROPERTIES", "MANAGED WAN", "CAPITAL GATE-ADNEC LEANING TOWER", "capitalgatetwrill-ra" });
dt.Rows.Add(new object[] { "27883481", "EAGLE HILLS PROPERTIES", "MANAGED WAN", "2020 BLDG", "dxbcontactcentreill-rb" });
dt.Rows.Add(new object[] { "27883481", "EAGLE HILLS PROPERTIES", "MANAGED WAN", "2020 BLDG", "dxbcontactcentreill-ra" });
dt.Rows.Add(new object[] { "27883481", "EAGLE HILLS PROPERTIES", "MANAGED WAN", "CAPITAL GATE-ADNEC LEANING TOWER", "capitalgatetwrill-rb" });
dt.Rows.Add(new object[] { "27883481", "EAGLE HILLS PROPERTIES", "MANAGED LAN", "CAPITAL GATE-ADNEC LEANING TOWER", "capitalgatetwrill-ra" });
dt.Rows.Add(new object[] { "27883481", "EAGLE HILLS PROPERTIES", "MANAGED LAN", "2020 BLDG", "dxbcontactcentreill-rb" });
dt.Rows.Add(new object[] { "27883481", "EAGLE HILLS PROPERTIES", "MANAGED LAN", "2020 BLDG", "dxbcontactcentreill-ra" });
dt.Rows.Add(new object[] { "27883481", "EAGLE HILLS PROPERTIES", "MANAGED LAN", "CAPITAL GATE-ADNEC LEANING TOWER", "capitalgatetwrill-rb" });
dt.Rows.Add(new object[] { "27883", "EAGLE DRILLS PROPERTIES", "MANAGED WAN", "CAPITAL GATE-ADNEC LEANING TOWER", "capitalgatetwrill-ra" });
dt.Rows.Add(new object[] { "27883", "EAGLE DRILLS PROPERTIES", "MANAGED WAN", "2020 BLDG", "dxbcontactcentreill-rb" });
dt.Rows.Add(new object[] { "27883", "EAGLE DRILLS PROPERTIES", "MANAGED WAN", "2020 BLDG", "dxbcontactcentreill-ra" });
dt.Rows.Add(new object[] { "27883", "EAGLE DRILLS PROPERTIES", "MANAGED WAN", "CAPITAL GATE-ADNEC LEANING TOWER", "capitalgatetwrill-rb" });
dt = dt.AsEnumerable()
.OrderBy(x => x.Field<string>("ID"))
.ThenBy(x => x.Field<string>("NAME"))
.ThenBy(x => x.Field<string>("SERVICE"))
.ThenBy(x => x.Field<string>("SITENAME"))
.ThenBy(x => x.Field<string>("NODENAME"))
.CopyToDataTable();
string xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><AllSites></AllSites>";
XDocument doc = XDocument.Parse(xml);
XElement allSites = doc.Root;
foreach (var idGroup in dt.AsEnumerable().GroupBy(x => x.Field<string>("ID")))
{
XElement siteNode = new XElement("SITE_NODE");
allSites.Add(siteNode);
siteNode.Add(new XElement("ID", idGroup.Key));
siteNode.Add(new XElement("NAME", idGroup.First().Field<string>("NAME")));
XElement partyServices = new XElement("PARTY_SERVICES");
siteNode.Add(partyServices);
partyServices.Add(new XElement("SERVICE_NAME", idGroup.First().Field<string>("SERVICE")));
foreach (var serviceSite in idGroup.GroupBy(x => x.Field<string>("SITENAME")))
{
XElement serviceSites = new XElement("SERVICE_SITES");
partyServices.Add(serviceSites);
serviceSites.Add(new XElement("SITE_NAME", serviceSite.Key));
XElement siteNodes = new XElement("SITE_NODES");
serviceSites.Add(siteNodes);
string[] nodeNames = serviceSite.Select(x => x.Field<string>("NODENAME")).Distinct().ToArray();
foreach (string nodeName in nodeNames)
{
siteNodes.Add(new XElement("NODE_NAME", nodeName));
}
}
}
doc.Save(FILENAME);
}
}
}
我有一个来自数据库的数据表,其中包含如下数据
ID NAME SERVICE SITENAME NODENAME
27883481 EAGLE HILLS PROPERTIES MANAGED WAN CAPITAL GATE-ADNEC LEANING TOWER capitalgatetwrill-ra
27883481 EAGLE HILLS PROPERTIES MANAGED WAN 2020 BLDG dxbcontactcentreill-rb
27883481 EAGLE HILLS PROPERTIES MANAGED WAN 2020 BLDG dxbcontactcentreill-ra
27883481 EAGLE HILLS PROPERTIES MANAGED WAN CAPITAL GATE-ADNEC LEANING TOWER capitalgatetwrill-rb
27883481 EAGLE HILLS PROPERTIES MANAGED LAN CAPITAL GATE-ADNEC LEANING TOWER capitalgatetwrill-ra
27883481 EAGLE HILLS PROPERTIES MANAGED LAN 2020 BLDG dxbcontactcentreill-rb
27883481 EAGLE HILLS PROPERTIES MANAGED LAN 2020 BLDG dxbcontactcentreill-ra
27883481 EAGLE HILLS PROPERTIES MANAGED LAN CAPITAL GATE-ADNEC LEANING TOWER capitalgatetwrill-rb
27883 EAGLE DRILLS PROPERTIES MANAGED WAN CAPITAL GATE-ADNEC LEANING TOWER capitalgatetwrill-ra
27883 EAGLE DRILLS PROPERTIES MANAGED WAN 2020 BLDG dxbcontactcentreill-rb
27883 EAGLE DRILLS PROPERTIES MANAGED WAN 2020 BLDG dxbcontactcentreill-ra
27883 EAGLE DRILLS PROPERTIES MANAGED WAN CAPITAL GATE-ADNEC LEANING TOWER capitalgatetwrill-rb
我想根据上面的数据创建 XMl,如下所示,这是 XML
的原型<?xml version="1.0" encoding="utf-8" ?>
<AllSites>
<SITE_NODES>
<ID>27883481</ID>
<NAME>EAGLE HILLS PROPERTIES""</NAME>
<PARTY_SERVICES>
<SERVICE_NAME>MANAGED WAN</SERVICE_NAME>
<SERVICE_SITES>
<SITE_NAME>CAPITAL GATE-ADNEC LEANING TOWER</SITE_NAME>
<SITE_NODES>
<NODE_NAME>capitalgatetwrill-ra</NODE_NAME>
<NODE_NAME>capitalgatetwrill-rb</NODE_NAME>
</SITE_NODES>
</SERVICE_SITES>
<SERVICE_SITES>
<SITE_NAME>2020 BLDG</SITE_NAME>
<SITE_NODES>
<NODE_NAME>dxbcontactcentreill-rb</NODE_NAME>
<NODE_NAME>dxbcontactcentreill-ra</NODE_NAME>
</SITE_NODES>
</SERVICE_SITES>
</PARTY_SERVICES>
<PARTY_SERVICES>
<SERVICE_NAME>MANAGED LAN</SERVICE_NAME>
<SERVICE_SITES>
<SITE_NAME>CAPITAL GATE-ADNEC LEANING TOWER</SITE_NAME>
<SITE_NODES>
<NODE_NAME>abcd-ra</NODE_NAME>
<NODE_NAME>abcd-rb</NODE_NAME>
</SITE_NODES>
</SERVICE_SITES>
<SERVICE_SITES>
<SITE_NAME>2021 BLDG</SITE_NAME>
<SITE_NODES>
<NODE_NAME>pqms-rb</NODE_NAME>
<NODE_NAME>pqms-ra</NODE_NAME>
</SITE_NODES>
</SERVICE_SITES>
</PARTY_SERVICES>
</SITE_NODES>
</AllSites>
如何生成它?我是否需要创建一个带有占位符的 XML 模板,应该在运行时替换它?或者创建 c# 类 并生成它 如下
public class DetailsBO
{
public string ID { get; set; }
public string NAME { get; set; }
public string SERVICE { get; set; }
public List<SitesBO> sites { get; set; }
}
public class SitesBO
{
public string SITENAME { get; set; }
public List<NodesBO> noodes { get; set; }
}
public class NodesBO
{
public string NODENAME { get; set; }
}
谁能给我c#代码
编辑
假设如果站点名称为空或为空,则所有站点都将成为与 WAN 或 LAN 相同的服务类型
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(string));
dt.Columns.Add("NAME", typeof(string));
dt.Columns.Add("SERVICE", typeof(string));
dt.Columns.Add("SITENAME", typeof(string));
dt.Columns.Add("NODENAME", typeof(string));
dt.Rows.Add(new object[] { "27883481", "EAGLE HILLS PROPERTIES", "MANAGED WAN", "", "capitalgatetwrill-ra" });
dt.Rows.Add(new object[] { "27883481", "EAGLE HILLS PROPERTIES", "MANAGED WAN", "2020 BLDG", "dxbcontactcentreill-rb" });
dt.Rows.Add(new object[] { "27883481", "EAGLE HILLS PROPERTIES", "MANAGED WAN", "2020 BLDG", "dxbcontactcentreill-ra" });
dt.Rows.Add(new object[] { "27883481", "EAGLE HILLS PROPERTIES", "MANAGED WAN", "CAPITAL GATE-ADNEC LEANING TOWER", "capitalgatetwrill-rb" });
dt.Rows.Add(new object[] { "27883481", "EAGLE HILLS PROPERTIES", "MANAGED LAN", "CAPITAL GATE-ADNEC LEANING TOWER", "capitalgatetwrill-ra" });
dt.Rows.Add(new object[] { "27883481", "EAGLE HILLS PROPERTIES", "MANAGED LAN", "2020 BLDG", "dxbcontactcentreill-rb" });
dt.Rows.Add(new object[] { "27883481", "EAGLE HILLS PROPERTIES", "MANAGED LAN", "2020 BLDG", "dxbcontactcentreill-ra" });
dt.Rows.Add(new object[] { "27883481", "EAGLE HILLS PROPERTIES", "MANAGED LAN", "CAPITAL GATE-ADNEC LEANING TOWER", "capitalgatetwrill-rb" });
dt.Rows.Add(new object[] { "27883", "EAGLE DRILLS PROPERTIES", "MANAGED WAN", "CAPITAL GATE-ADNEC LEANING TOWER", "capitalgatetwrill-ra" });
dt.Rows.Add(new object[] { "27883", "EAGLE DRILLS PROPERTIES", "MANAGED WAN", "2020 BLDG", "dxbcontactcentreill-rb" });
dt.Rows.Add(new object[] { "27883", "EAGLE DRILLS PROPERTIES", "MANAGED WAN", "2020 BLDG", "dxbcontactcentreill-ra" });
dt.Rows.Add(new object[] { "27883", "EAGLE DRILLS PROPERTIES", "MANAGED WAN", "CAPITAL GATE-ADNEC LEANING TOWER", "capitalgatetwrill-rb" });
最简单的代码就是取当前数据table直接输出到xml。您可以修改代码以使用将创建 类 并序列化 类 的实体。但是因为你已经有了数据table,所以我直接拿了table然后xml。看起来您删除了重复项,所以我也这样做了。结果我也下单了
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(string));
dt.Columns.Add("NAME", typeof(string));
dt.Columns.Add("SERVICE", typeof(string));
dt.Columns.Add("SITENAME", typeof(string));
dt.Columns.Add("NODENAME", typeof(string));
dt.Rows.Add(new object[] { "27883481", "EAGLE HILLS PROPERTIES", "MANAGED WAN", "CAPITAL GATE-ADNEC LEANING TOWER", "capitalgatetwrill-ra" });
dt.Rows.Add(new object[] { "27883481", "EAGLE HILLS PROPERTIES", "MANAGED WAN", "2020 BLDG", "dxbcontactcentreill-rb" });
dt.Rows.Add(new object[] { "27883481", "EAGLE HILLS PROPERTIES", "MANAGED WAN", "2020 BLDG", "dxbcontactcentreill-ra" });
dt.Rows.Add(new object[] { "27883481", "EAGLE HILLS PROPERTIES", "MANAGED WAN", "CAPITAL GATE-ADNEC LEANING TOWER", "capitalgatetwrill-rb" });
dt.Rows.Add(new object[] { "27883481", "EAGLE HILLS PROPERTIES", "MANAGED LAN", "CAPITAL GATE-ADNEC LEANING TOWER", "capitalgatetwrill-ra" });
dt.Rows.Add(new object[] { "27883481", "EAGLE HILLS PROPERTIES", "MANAGED LAN", "2020 BLDG", "dxbcontactcentreill-rb" });
dt.Rows.Add(new object[] { "27883481", "EAGLE HILLS PROPERTIES", "MANAGED LAN", "2020 BLDG", "dxbcontactcentreill-ra" });
dt.Rows.Add(new object[] { "27883481", "EAGLE HILLS PROPERTIES", "MANAGED LAN", "CAPITAL GATE-ADNEC LEANING TOWER", "capitalgatetwrill-rb" });
dt.Rows.Add(new object[] { "27883", "EAGLE DRILLS PROPERTIES", "MANAGED WAN", "CAPITAL GATE-ADNEC LEANING TOWER", "capitalgatetwrill-ra" });
dt.Rows.Add(new object[] { "27883", "EAGLE DRILLS PROPERTIES", "MANAGED WAN", "2020 BLDG", "dxbcontactcentreill-rb" });
dt.Rows.Add(new object[] { "27883", "EAGLE DRILLS PROPERTIES", "MANAGED WAN", "2020 BLDG", "dxbcontactcentreill-ra" });
dt.Rows.Add(new object[] { "27883", "EAGLE DRILLS PROPERTIES", "MANAGED WAN", "CAPITAL GATE-ADNEC LEANING TOWER", "capitalgatetwrill-rb" });
dt = dt.AsEnumerable()
.OrderBy(x => x.Field<string>("ID"))
.ThenBy(x => x.Field<string>("NAME"))
.ThenBy(x => x.Field<string>("SERVICE"))
.ThenBy(x => x.Field<string>("SITENAME"))
.ThenBy(x => x.Field<string>("NODENAME"))
.CopyToDataTable();
string xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><AllSites></AllSites>";
XDocument doc = XDocument.Parse(xml);
XElement allSites = doc.Root;
foreach (var idGroup in dt.AsEnumerable().GroupBy(x => x.Field<string>("ID")))
{
XElement siteNode = new XElement("SITE_NODE");
allSites.Add(siteNode);
siteNode.Add(new XElement("ID", idGroup.Key));
siteNode.Add(new XElement("NAME", idGroup.First().Field<string>("NAME")));
XElement partyServices = new XElement("PARTY_SERVICES");
siteNode.Add(partyServices);
partyServices.Add(new XElement("SERVICE_NAME", idGroup.First().Field<string>("SERVICE")));
foreach (var serviceSite in idGroup.GroupBy(x => x.Field<string>("SITENAME")))
{
XElement serviceSites = new XElement("SERVICE_SITES");
partyServices.Add(serviceSites);
serviceSites.Add(new XElement("SITE_NAME", serviceSite.Key));
XElement siteNodes = new XElement("SITE_NODES");
serviceSites.Add(siteNodes);
string[] nodeNames = serviceSite.Select(x => x.Field<string>("NODENAME")).Distinct().ToArray();
foreach (string nodeName in nodeNames)
{
siteNodes.Add(new XElement("NODE_NAME", nodeName));
}
}
}
doc.Save(FILENAME);
}
}
}