0

i am reading a xml file and querying by LINQ this below way

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;

but getting error No property or field 'OrderID' exists in type 'XElement' (at index 0)

this is my sample xml which i am querying by LINQ

<?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>

i used this below query but still no luck

var query = document.Descendants("orders")
                    .OrderBy(String.Format("Element(\"{0}\").Value {1}", sortColumn, OrderDirection))
                    .Skip(lowerPageBoundary - 1 * rowsPerPage)
                    .Take(rowsPerPage);
2
  • Have you tried document.root.Descendants("Orders") Commented Jul 22, 2015 at 18:44
  • @w.b i used dynamic link and there that kind of order by clause is valid. Commented Jul 22, 2015 at 19:16

1 Answer 1

2

The reason you are getting the error is because XML tags are case sensitive.

An excerpt from the link (modified to match the example in your question):

XML tags are case sensitive. The tag <Orders> is different from the tag <orders>.

Your query is searching for the non-existing element "orders". Update your query:

XDocument document = XDocument.Load(xmlFilePath);
var query = document.Descendants("Orders").Select(c => c);

UPDATE:

Based on your comments, the error is actually rooted in attempting to order by "OrderID" within the IEnumerabe<XElement>. The "OrderID" is actually a child element of each XElement.

Conducting some more research on this would probably be helpful. With the help of google search, my results show that the System.Linq.Dynamic library was not designed for use with XML.

See this SO Question where the OP concludes:

I finally got it working. I abandoned my original approach because as of now I'm not convinced it was even intended for use with Xml. I've seen little posted anywhere to argue against that statement.

What about reading the XML into a DataSet?

DataSet dataSet = new DataSet();
dataSet.ReadXml(xmlFilePath);

string sort = sortColumn + " " + OrderDirection;

DataTable table = dataSet.Tables["Orders"].Select("", sort)
                                          .Skip(lowerPageBoundary - 1 * rowsPerPage)
                                          .Take(rowsPerPage)
                                          .CopyToDataTable();

table.Locale = System.Globalization.CultureInfo.InvariantCulture;
Sign up to request clarification or add additional context in comments.

3 Comments

sorry it was type i used Orders not orders but still got error
see last code where string.format was used in order by clause
It would be helpful to all if you updated your question to reflect your comments here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.