load xelement into a datatable

点点圈 提交于 2019-12-18 09:39:16

问题


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>

回答1:


try

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


来源:https://stackoverflow.com/questions/7602581/load-xelement-into-a-datatable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!