我正在尝试将xml文件加载到datable中,但我不知道从哪里开始。 Xml文件包含有助于标识标题,行和列的标记。以下是一个示例:
<result>
<info>
<title>Summary By Employee: My Default</title>
<sorted-by>Employee Name Ascending</sorted-by>
<filtered-by>Employee: All; Dates: 01/11/2015-01/17/2015</filtered-by>
<date>01/21/2015</date>
<time>08:40a</time>
<generated-by>Joe Doe </generated-by>
</info>
<header>
<col>
<label>Employee Id</label>
</col>
<col>
<label>Employee Name</label>
</col>
<col>
<label>Regular</label>
</col>
<col>
<label>Overtime</label>
</col>
<col>
<label>Premium</label>
</col>
<col>
</header>
<body>
<row>
<col>022692</col>
<col>James, Mitchel</col>
<col>-</col>
<col>-</col>
<col>-</col>
</row>
<row>
<col>022685</col>
<col>Doe, Joe</col>
<col>-</col>
<col>-</col>
<col>-</col>
</row>
关于如何实现这一目标的任何想法?
感谢您的帮助。
答案 0 :(得分:0)
尝试以下代码。您需要使用 System.Xml.Linq 。
string xml = @"<result>
<info>
<title>Summary By Employee: My Default</title>
<sorted-by>Employee Name Ascending</sorted-by>
<filtered-by>Employee: All; Dates: 01/11/2015-01/17/2015</filtered-by>
<date>01/21/2015</date>
<time>08:40a</time>
<generated-by>Joe Doe </generated-by>
</info>
<header>
<col>
<label>Employee Id</label>
</col>
<col>
<label>Employee Name</label>
</col>
<col>
<label>Regular</label>
</col>
<col>
<label>Overtime</label>
</col>
<col>
<label>Premium</label>
</col>
</header>
<body>
<row>
<col>022692</col>
<col>James, Mitchel</col>
<col>-</col>
<col>-</col>
<col>-</col>
</row>
<row>
<col>022685</col>
<col>Doe, Joe</col>
<col>-</col>
<col>-</col>
<col>-</col>
</row></body></result>";
StringReader xmlReader= new StringReader(xml);
DataSet ds= new DataSet();
ds.ReadXml(xmlReader);
DataTable result = ds.Tables[0];
以上将产生以下数据集......