将xelement加载到数据表中
我有以下xml文件,其中包含很多关于公司分支机构的信息..(这只是一个例子)..
我真正需要的是,只在数据表中加载Branch1中的数据(与我的xml文件具有相同的结构,因此数据表根本没有问题)。
iam使用c#而我想这样做是linq,但我不知道linq …我的问题是:我如何从xml读取条目作为数据表行,所以我可以将它复制到我的数据表?
我现在有:
XElement main = XElement.Load("branches.xml"); IEnumerable 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);
任何帮助真的很感激..
Street 1, 1234, NY 0123456789 James Street 2, 4567, NY 9876543210 Will
尝试
branchesDataTable.ReadXml(new StringReader(new XElement("branches", elList).ToString()));