将 xelement 加载到数据表中

leb*_*ero 3 c# xml linq-to-xml

我有以下 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);
Run Code Online (Sandbox Code Playgroud)

任何帮助真的很感激..

<?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>
Run Code Online (Sandbox Code Playgroud)

Bal*_*a R 6

尝试

branchesDataTable.ReadXml(new StringReader(new XElement("branches", elList).ToString()));
Run Code Online (Sandbox Code Playgroud)