locked
I have a xml file of 1000 rows How to fetch first 50 rows? RRS feed

  • Question

  • User-1313990200 posted

    How to fetch the first 50 rows from a XML file?

    Friday, October 18, 2019 12:27 PM

All replies

  • User1577371250 posted

    Hello,

    Here is the sample to get the data from the xml file

    <?xml version="1.0" encoding="utf-8" ?>
    <Employees>
     <Employee>
        <EmpId>1</EmpId>
        <Name>Sam</Name>   
     </Employee>
     <Employee>
        <EmpId>2</EmpId>
        <Name>Lucy</Name>
       </Employee>
     <Employee>
        <EmpId>3</EmpId>
        <Name>Kate</Name>
       </Employee>
     <Employee>
        <EmpId>4</EmpId>
        <Name>Chris</Name>
      </Employee>
    </Employees>
    XDocument xmlDocument = XDocument.Load(@"E:\employees.xml"); // path of the xml file
    var employees = xmlDocument.Descendants("Employee").Take(2); // instead of 2 use 50 or any number
    

    Friday, October 18, 2019 5:21 PM
  • User-1313990200 posted

    Does the code work for the xml structure if it is different than what you have posted?


    <response xmlns="http://business.govSearch/"><usageStatement>XXX</usageStatement><dateRegisterLastUpdated>2019-10-19</dateRegisterLastUpdated><dateTimeRetrieved>2019-10-18T19:21:42.0766291+05:30</dateTimeRetrieved><searchResultsList><numberOfRecords>201</numberOfRecords><exceedsMaximum>N</exceedsMaximum><searchResultsRecord><ABN><identifierValue>11005357522</identifierValue><identifierStatus>Active</identifierStatus></ABN><businessName><organisationName>ANZ</organisationName><score>100</score><isCurrentIndicator>Y</isCurrentIndicator></businessName><mainBusinessPhysicalAddress><stateCode>VIC</stateCode><postcode>3008</postcode>

    Saturday, October 19, 2019 12:32 PM
  • User-719153870 posted

    Hi chandu123,

    Does the code work for the xml structure if it is different than what you have posted?

    It should work and you can just test it. You can also refer to select top 5 records from xml with C# for more information about using Linq to get items from xml document.

    But for your xml file, it seems a little confusing that which 50 rows you mean you want to fecth, this method will work for items but not sure what about the rows.

    As far as i know, these methods are based on nodes instead of rows, maybe you could provide a more detailed description.

    Best Regard,

    Yang Shen

    Monday, October 21, 2019 6:41 AM
  • User-1313990200 posted

    I have a reponse like i have pasted below.so i need to take first 50 rows from the response.

    <response xmlns="http://abr.business.gov/">
    <usageStatement>.</usageStatement>
    <dateRegisterLastUpdated>2019-10-19</dateRegisterLastUpdated>
    <dateTimeRetrieved>2019-10-18T19:20:49.7493391+05:30</dateTimeRetrieved>
    <businessEntity>
    <recordLastUpdatedDate>2019-07-03</recordLastUpdatedDate>
    <ABN>
    <identifierValue>53004085616</identifierValue>
    <isCurrentIndicator>Y</isCurrentIndicator>
    <replacedFrom>0001-01-01</replacedFrom>
    </ABN>
    <entityStatus>
    <entityStatusCode>Active</entityStatusCode>
    <effectiveFrom>1999-11-01</effectiveFrom>
    <effectiveTo>0001-01-01</effectiveTo>
    </entityStatus>
    <ASICNumber>004085616</ASICNumber>
    <entityType>
    <entityTypeCode>PRV</entityTypeCode>
    <entityDescription>Australian Private Company</entityDescription>
    </entityType>

    Monday, October 21, 2019 6:58 AM
  • User-719153870 posted

    Hi chandu123,

    In your case, this could be complex, the xmlDocument.Descendants("Employee").Take(2); requires a specific node name which hard to tell in your xml sample.

    Also, as i said before, there's no rows in xml but nodes, are you trying to get the first 50 <businessEntity>?

    Or, if you want to get all tags from your xml file, you will need to add a same parent node before every tags in <response>..</response>.

    It's hard to understand what exactly you want to fecth, thus we couldn't provide the efficient way to help.

    Best Regard,

    Yang Shen

    Monday, October 21, 2019 8:04 AM
  • User-1313990200 posted

    The XML looks like this 

    <Employees>
     <Employee>
        <EmpId>1</EmpId>
        <Name>Sam</Name>   
     </Employee>
     <Employee>
        <EmpId>2</EmpId>
        <Name>Lucy</Name>
       </Employee>
     <Employee>
        <EmpId>3</EmpId>
        <Name>Kate</Name>
       </Employee>
     <Employee>
        <EmpId>4</EmpId>
        <Name>Chris</Name>
      </Employee>
    </Employees>

    So i want to fetch the first 50 records of the XML.
    Tuesday, October 22, 2019 3:34 AM
  • User-719153870 posted

    Hi chandu123,

    In this case, the solution provided by @Lokesh B R will work, as you can see in this xml file, there's a root node called <Employees> and there are several <Employee> nodes in it.

    So i want to fetch the first 50 records of the XML.

    I think what you really mean is that you want to fetch the first 50 <Employee> items from your xml file, in this standard format xml file, xmlDocument.Descendants("Employee").Take(50); will achieve your goal.

    But not in your previous ones because of the format issue.

    Best Regard,

    Yang Shen

    Tuesday, October 22, 2019 5:40 AM