locked
No property or field 'OrderID' exists in type 'XElement' (at index 0) RRS feed

  • Question

  • User264732274 posted

    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>

    in debug mode i expand the result view and found order id exist. here is screen shot. enter image description here

    so tell me where i made the mistake in code. please guide. thanks

    Tuesday, July 21, 2015 11:11 AM

Answers

  • User-271186128 posted

    Hi sudip_inn,

    From your code and description, it seems that you want to read XML file using LINQ, and achieve paging and sorting. If that is the case, please refer to the following sample:

    XML file:

    <?xml version="1.0" encoding="utf-8"?>
    <Setting>
      <Details>
        <id>10004</id>
        <posted-date>2015-07-02</posted-date>
        <source>CCC.com</source>
        <summary>Hello CCC</summary>
      </Details>
      <Details>
        <id>10005</id>
        <posted-date>2015-07-03</posted-date>
        <source>DDD.com</source>
        <summary>Hello DDD</summary>
      </Details>
    
      <Details>
        <id>10003</id>
        <posted-date>2015-07-01</posted-date>
        <source>BBB.com</source>
        <summary>Hello BBB</summary>
      </Details>
    </Setting>

    Code in page (.aspx.cs)

            protected void Page_Load(object sender, EventArgs e)
            {
                XElement xmldoc = XElement.Load(Server.MapPath("XMLFile1.xml"));
    
                var query = xmldoc.Descendants("Details")
                    .OrderBy(cc => cc.Element("posted-date").Value)
                    .Select(cc => new { 
                        ID = cc.Element("id").Value, 
                        PostedDate = cc.Element("posted-date").Value, 
                        Source = cc.Element("source").Value, 
                        Summary = cc.Element("summary").Value })
                        .Skip(2)
                        .Take(3);
    
                GridView1.DataSource = query.ToList();
                GridView1.DataBind();
    
            }

    Best Regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 21, 2015 10:58 PM

All replies

  • User-434868552 posted

    @sudip_inn

    You seem to be passing "OrderID" via sortColumn ...

    in debug mode, check the value of sortColumn ... is it an upper case sans-serif "i", or a lower case letter 'L'?

    Tuesday, July 21, 2015 11:59 AM
  • User264732274 posted

    u said "You seem to be passing "OrderID" via sortColumn" yes i am passing OrderID as wort column and rest of you text is not clear like what u r trying to ask me. thanks

    Tuesday, July 21, 2015 1:55 PM
  • User-434868552 posted

    @sudip_inn

    i am asking you to break on this line and then to check the actual value of sortColumn:

    query = query.OrderBy(sortColumn + " " + OrderDirection);

    is the debug value of sortColumn "OrderID" (upper case letter "i") or "OrderlD"  (lower case letter "L") 

    i.e., have you made a typo?

    Tuesday, July 21, 2015 3:08 PM
  • User264732274 posted

    Sort column value was "OrderID" i guess.....i will check and let u know tomorrow. thanks

    Tuesday, July 21, 2015 3:46 PM
  • User-271186128 posted

    Hi sudip_inn,

    From your code and description, it seems that you want to read XML file using LINQ, and achieve paging and sorting. If that is the case, please refer to the following sample:

    XML file:

    <?xml version="1.0" encoding="utf-8"?>
    <Setting>
      <Details>
        <id>10004</id>
        <posted-date>2015-07-02</posted-date>
        <source>CCC.com</source>
        <summary>Hello CCC</summary>
      </Details>
      <Details>
        <id>10005</id>
        <posted-date>2015-07-03</posted-date>
        <source>DDD.com</source>
        <summary>Hello DDD</summary>
      </Details>
    
      <Details>
        <id>10003</id>
        <posted-date>2015-07-01</posted-date>
        <source>BBB.com</source>
        <summary>Hello BBB</summary>
      </Details>
    </Setting>

    Code in page (.aspx.cs)

            protected void Page_Load(object sender, EventArgs e)
            {
                XElement xmldoc = XElement.Load(Server.MapPath("XMLFile1.xml"));
    
                var query = xmldoc.Descendants("Details")
                    .OrderBy(cc => cc.Element("posted-date").Value)
                    .Select(cc => new { 
                        ID = cc.Element("id").Value, 
                        PostedDate = cc.Element("posted-date").Value, 
                        Source = cc.Element("source").Value, 
                        Summary = cc.Element("summary").Value })
                        .Skip(2)
                        .Take(3);
    
                GridView1.DataSource = query.ToList();
                GridView1.DataBind();
    
            }

    Best Regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 21, 2015 10:58 PM