简体   繁体   English

嵌套树(或XML)到数据表

[英]Nested Tree (or XML) to datatable

I have a nested XML file like this 我有一个这样的嵌套XML文件

<students>
    <student id=1>
        <course cid=1 name="cse" />
        <course cid=2 name="eee" />
        <course cid=3 name="mech" />
        <course cid=4 name="it" />
    </studnet>
    <student id=2>
        <course cid=1 name="cse" />
        <course cid=2 name="eee" />
        <course cid=3 name="mech" />
        <course cid=4 name="it" />
    </studnet>

I want to save it in datatable in this format 我想以这种格式将其保存在数据表中

id    cid    name
1     1      cse
1     2      ece
1     3      mech
1     4      it
2     1      cse
2     2      eee
2     3      mech
2     4      it

Currently I am parsing the XML file and strong it in a tree datastructure 目前,我正在解析XML文件并将其增强为树数据结构

class Node{
    string Text;
    List<Attribute> Attributes;
    List<Node> Children;
}
class Attribute{
    string name;
    string value;
}

I can parse the XML and store it in any Data structure. 我可以解析XML并将其存储在任何Data结构中。 The XML file can have any format and attributes, thats why I have used Children of Type Attribute and not defined the specific property(id, cis etc) for the class. XML文件可以具有任何格式和属性,这就是为什么我使用了Type of Attribute的Children而不定义类的特定属性(id,cis等)的原因。

Any idea how I can convert my tree created from XML to datatable. 知道如何将从XML创建的树转换为数据表。 I plan to bind this datarable and bind it to datagrid and again save it into XML. 我计划绑定此datarable并将其绑定到datagrid,然后再次将其保存为XML。

To bind XML to datatable you can use this 要将XML绑定到数据表,您可以使用此

DataTable dt = new DataTable();
 dt.ReadXml();

Take a look at some of these simple examples, they Serialize a class to and from XML. 看一下其中的一些简单示例,它们将类与XML序列化。 this example returns a List which can the be used a DataSource of the DataGrid 本示例返回一个List,该列表可以用作DataGrid的DataSource

http://nakdev.somee.com/#1&C54B782F02A748E0BCCDFE50B0586BA2&cs http://nakdev.somee.com/#1&C54B782F02A748E0BCCDFE50B0586BA2&cs

And this example demonstrates setting the DataSource of the DataGrid to a DataSet 此示例演示了如何将DataGrid的DataSource设置为DataSet

http://nakdev.somee.com/#2&1BC37606DFD843EC94249A93B4BD17A2&cs http://nakdev.somee.com/#2&1BC37606DFD843EC94249A93B4BD17A2&cs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM