简体   繁体   中英

Most efficient way to read two nodes from multiple XML files?

I'm looking for a fast and efficient way to retreive 2 strings from multiple xml files.

The files them selves aren't that big only 50kb to 100kb and the number of files can range from none to 100. I've included a sample of a very small xml file i'll be using. All xml files will be the same format and there's only 2 things i need to know from all files namely:

    <Company>test bv.</Company>
    <Ship>sss testing</Ship>

i want to store these strings in a struct/class since i cannot use Tuple in .net 3.5 (or am i mistaking?)

so the question: what is the most efficient way to do this? using Xdocument? using xmlReader? or something else?

<?xml version="1.0" encoding="utf-8"?>
<Collection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Customer>
    <ID>-1</ID>
    <Updated>true</Updated>
    <Company>test bv.</Company>
    <Ship>sss testing</Ship>
    <Adress>fggfgggff gvg</Adress>
    <City>fgcgg</City>
    <ZipCode>5454fc</ZipCode>
    <Country>fcgggff</Country>
    <BTW_Nmbr>55</BTW_Nmbr>
    <IsTempCustomer>true</IsTempCustomer>
    <PhoneNumbers>
      <ContactData>
        <ID>21</ID>
        <Updated>true</Updated>
        <Description>455656567</Description>
        <Name>ghbbvh</Name>
        <IsSendable>false</IsSendable>
      </ContactData>
      <ContactData>
        <ID>22</ID>
        <Updated>true</Updated>
        <Description>2315098234146124302134</Description>
        <Name>asdfawegaebf</Name>
        <IsSendable>false</IsSendable>
      </ContactData>
    </PhoneNumbers>
    <EmailAdresses />
  </Customer>
  <Order>
    <ID>-1</ID>
    <Updated>true</Updated>
    <OrderNr>10330200</OrderNr>
    <OrderDate>1-1-2005</OrderDate>
    <StartDate>10-9-2013</StartDate>
    <ExpirationDate>20-10-2013</ExpirationDate>
    <Executor>Andre de Zwart</Executor>
    <Executors />
    <Reference />
    <OrderDetail />
    <IsDigital>true</IsDigital>
  </Order>
  <Materials>
    <MaterialData>
      <ID>108</ID>
      <Updated>true</Updated>
      <Description>ffdffggfg</Description>
      <Amount>34</Amount>
    </MaterialData>
    <MaterialData>
      <ID>109</ID>
      <Updated>true</Updated>
      <Description>ffccff</Description>
      <Amount>45</Amount>
    </MaterialData>
  </Materials>
  <HourExpenses>
    <HourExpensesData>
      <ID>43850</ID>
      <Updated>true</Updated>
      <Date>2013-10-06T00:00:00</Date>
      <Notes>lala</Notes>
      <Day>Sunday</Day>
      <Hours>0.01</Hours>
      <BusinessHours>0.01</BusinessHours>
      <TravledHoursTo>0</TravledHoursTo>
      <TravledHoursFrom>0</TravledHoursFrom>
      <Start>2013-10-06T12:27:00</Start>
      <Stop>2013-10-06T12:27:00</Stop>
    </HourExpensesData>
    <HourExpensesData>
      <ID>43849</ID>
      <Updated>true</Updated>
      <Date>2013-09-17T00:00:00</Date>
      <Notes>oke dus ik ben nu lekker aan het werk en ik typ wat spul er bij</Notes>
      <Day>Tuesday</Day>
      <Hours>0</Hours>
      <BusinessHours>0.01</BusinessHours>
      <TravledHoursTo>0</TravledHoursTo>
      <TravledHoursFrom>0</TravledHoursFrom>
      <Start>2013-09-17T12:31:31</Start>
      <Stop>2013-09-17T12:31:32</Stop>
    </HourExpensesData>
    <HourExpensesData>
      <ID>43855</ID>
      <Updated>true</Updated>
      <Date>2013-10-03T00:00:00</Date>
      <Notes>test</Notes>
      <Day>Thursday</Day>
      <Hours>0</Hours>
      <BusinessHours>0</BusinessHours>
      <TravledHoursTo>12</TravledHoursTo>
      <TravledHoursFrom>12</TravledHoursFrom>
      <Start>0001-01-01T00:00:00</Start>
      <Stop>0001-01-01T00:00:00</Stop>
    </HourExpensesData>
  </HourExpenses>
  <TravelExpenses>
    <TravelExpensesData>
      <ID>672</ID>
      <Updated>true</Updated>
      <Date>2013-09-27T00:00:00</Date>
      <Notes />
      <KmTo>45</KmTo>
      <KmFrom>45</KmFrom>
      <Declaration>0</Declaration>
    </TravelExpensesData>
  </TravelExpenses>
  <Signatures>
    <ID>-1</ID>
    <Updated>true</Updated>
    <OrderID>10330200</OrderID>
    <Completed>false</Completed>
    <Notes>yay het werkt ;D</Notes>
  </Signatures>
  <RemovedDataList />
</Collection>

I would just use LINQ to XML in the simplest possible way:

var query = from file in files
            let doc = XDocument.Load(file)
            from customer in doc.Descendants("Customer")
            select new {
               Company = (string) customer.Element("Company"),
               Ship = (string) customer.Element("Ship")
            };

I'd expect that to be pretty quick already - but you should work out what your exact performance requirements are, then test them. (You almost certainly don't need "the most efficient" way - you need a sufficiently efficient way, whilst keeping the code readable.)

Note that if you want the values to be propagated out of the current method, you should create your own class to represent the company/ship pair.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM