将 xelement 加载到数据表中
我有以下 xml 文件,其中包含很多有关公司分支机构的信息.. (这只是一个例子)..
我真正需要的是仅加载数据表中 Branch1 的数据(与我的 xml 文件具有相同的结构,因此数据表根本没有问题)..
iam 使用 c#我想做的是 linq,但我不知道 linq... 我的问题是: 我如何从 xml 中读取条目作为数据表行,以便将其复制到我的数据表中?
我现在:
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>
i have the following xml file, that contains a lot of Information about branches of a company ..
(this is only an example)..
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 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 ?
i now have:
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);
any help is really appreciated ..
<?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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试
try