【发布时间】:2015-07-22 18:25:12
【问题描述】:
我正在读取一个 xml 文件并通过以下方式通过 LINQ 查询
XDocument document = XDocument.Load(xmlFilePath);
var query = document.Descendants("orders").Select(c => c);
query = query.OrderBy(sortColumn + " " + OrderDirection);
query = query.Skip(lowerPageBoundary - 1 * rowsPerPage).Take(rowsPerPage);
DataTable table = query.ToList().ConvertToDataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
//adapter.Fill(table);
return table;
但出现错误“XElement”类型中不存在任何属性或字段“OrderID”(在索引 0 处)
这是我通过 LINQ 查询的示例 xml
<?xml version="1.0" encoding="utf-8"?>
<Root>
<Orders>
<OrderID>10248</OrderID>
<CustomerID>VINET</CustomerID>
<EmployeeID>5</EmployeeID>
<OrderDate>1996-07-04T00:00:00</OrderDate>
<RequiredDate>1996-08-01T00:00:00</RequiredDate>
<ShippedDate>1996-07-16T00:00:00</ShippedDate>
<ShipVia>3</ShipVia>
<Freight>32.3800</Freight>
<ShipName>Vins et alcools Chevalier</ShipName>
<ShipAddress>59 rue de l'Abbaye</ShipAddress>
<ShipCity>Reims</ShipCity>
<ShipPostalCode>51100</ShipPostalCode>
<ShipCountry>France</ShipCountry>
</Orders>
</Root>
我在下面的查询中使用了这个,但仍然没有运气
var query = document.Descendants("orders")
.OrderBy(String.Format("Element(\"{0}\").Value {1}", sortColumn, OrderDirection))
.Skip(lowerPageBoundary - 1 * rowsPerPage)
.Take(rowsPerPage);
【问题讨论】:
-
你试过 document.root.Descendants("Orders")
-
@w.b 我使用了动态链接,这种 order by 子句是有效的。
标签: c# xml linq dynamic-linq