将XML文件转换为数据表C# [英] Convert XML file to datatable C#

查看:102
本文介绍了将XML文件转换为数据表C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下xml文件:

<?xml version="1.0" encoding="utf-8" ?>
<font id="plejd">
<glyph unicode="&#x100;" glyph-name="area_add" d="string" />
<glyph unicode="&#x101;" glyph-name="area_bathroom" d="string2" />
<glyph unicode="&#x102;" glyph-name="area_cabin" d="string3" />
<glyph unicode="&#x103;" glyph-name="area_cinema" d="string4" />
</font>

我想遍历每一项并添加unicode,字形名称&

I would like to iterate through every item and add the unicode, glyph-name & d to a datatable.

我有以下代码,但我不知道如何获取信息。

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())
        {

        }

如何实现?

推荐答案

尝试一下。.

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

这篇关于将XML文件转换为数据表C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆