我有以下 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()));
