[英]Convert XML file to datatable C#
I've the following xml file: 我有以下xml文件:
<?xml version="1.0" encoding="utf-8" ?>
<font id="plejd">
<glyph unicode="Ā" glyph-name="area_add" d="string" />
<glyph unicode="ā" glyph-name="area_bathroom" d="string2" />
<glyph unicode="Ă" glyph-name="area_cabin" d="string3" />
<glyph unicode="ă" glyph-name="area_cinema" d="string4" />
</font>
I would like to iterate through every item and add the unicode, glyph-name & d to a datatable. 我想遍历每一项并将unicode,字形名称和d添加到数据表中。
I have the following code but I don't know how to get the info. 我有以下代码,但我不知道如何获取信息。
XDocument xdoc = XDocument.Load("PivotIcons.xml");
foreach (XElement element in xdoc.Descendants())
{
}
How can I achieve this? 我该如何实现?
something like: 就像是:
XElement root = XElement.Load("PivotIcons.xml");
foreach (XElement element in root.Descendants("glyph"))
{
string unicode = element.Attribute("unicode").Value;
string glyphname = element.Attribute("glyph-name").Value;
string d = element.Attribute("d").Value;
// save to database...?
}
would do the job. 会做的工作。 You'll need to format the unicode to how you plan on storing it in the database!
您需要格式化unicode格式,以使其计划将其存储在数据库中!
Try this.. 尝试这个..
public static void XMLToDataTable()
{
XDocument doc = XDocument.Load(@"D:\\Sample.xml");
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.Add("unicode");
dt.Columns.Add("glyph_name");
dt.Columns.Add("d");
string[] arr = new string[3];
var dr = from n in doc.Descendants("glyph")
select new string[]{
arr[0] = n.Attribute("unicode").Value,
arr[1] = n.Attribute("glyph-name").Value,
arr[2] = n.Attribute("d").Value
};
foreach (var item in dr)
{
dt.Rows.Add(item[0],item[1],item[2]);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.