gpt4 book ai didi

c# - 将 xelement 加载到数据表中

转载 作者:太空宇宙 更新时间:2023-11-03 22:08:20 25 4
gpt4 key购买 nike

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

我真正需要的是只加载来自 Branch1 的数据到数据表中(与我的 xml 文件具有相同的结构,所以数据表完全没有问题)..

我正在使用 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>

最佳答案

尝试

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

关于c# - 将 xelement 加载到数据表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7602581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com