将xelement加载到数据表中 [英] load xelement into a datatable

查看:76
本文介绍了将xelement加载到数据表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下xml文件,其中包含有关公司分支机构的大量信息. (这只是一个例子).

i have the following xml file, that contains a lot of Information about branches of a company .. (this is only an example)..

我真正需要的是仅将Branch1中的数据加载到数据表中(该数据表与我的xml文件具有相同的结构,因此该数据表根本没有问题)..

what i really need, is to load only the data from Branch1 in a datatable(that has the same structure as my xml file, so no problem with the datatable at all) ..

iam使用C#,我想这样做是linq,但是我不知道linq ... 我的问题是: 我将如何从xml作为数据表行读取条目,以便将其复制到我的数据表中?

iam using c# and i would like to do this is linq, but i have no idea about linq... my question is: how would i read the entry from xml as a datatable row, so i can copy it to my datatable ?

我现在有:

XElement main = XElement.Load("branches.xml");
IEnumerable<XElement> elList =
from el in main.Descendants("branch").Where(ex=>ex.Attribute("name").Value=="Branch1")
select el;
//this will return me the element where name =Branch1
//now, how would i only load this entry into my datatable ??
//this won`t work
branchesDataTable.ReadXml(XElement el in elList);

非常感谢您的帮助.

<?xml version="1.0" encoding="utf-8"?>
<branches>
<branch name="Branch1">
    <address>Street 1, 1234, NY</address>
    <tel>0123456789</tel>
    <director>James</director>
</branch>   
<branch name="Branch2">
    <address>Street 2, 4567, NY</address>
    <tel>9876543210</tel>
    <director>Will</director>
</branch>
</branches>

推荐答案

尝试

branchesDataTable.ReadXml(new StringReader(new XElement("branches", elList).ToString()));

这篇关于将xelement加载到数据表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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