Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Task 2 – Querying by Reading in an XML File

For this task, you will query over a set of customers to find those that reside in London. However as the set of customers increases, you are less likely to store that data in a code file; rather you may choose to store the information in a data file, such as an XML file. Even though the data source is changing, the query structure remains the same.

 

1. This exercise uses the following XML file. Save the following as Customers.xml in the \bin\debug folder located in the current Project folder (by default this should be in …\My Documents\Visual Studio 2008\Projects\ LINQ Overview\ LINQ Overview\bin\debug):

 

<Customers>

<Customer CustomerID="ALFKI" City="Berlin" ContactName="Maria Anders" />

<Customer CustomerID="BONAP" City="Marseille" ContactName="Laurence Lebihan" />

<Customer CustomerID="CONSH" City="London" ContactName="Elizabeth Brown" />

<Customer CustomerID="EASTC" City="London" ContactName="Ann Devon" />

<Customer CustomerID="FRANS" City="Torino" ContactName="Paolo Accorti" />

<Customer CustomerID="LONEP" City="Portland" ContactName="Fran Wilson" />

<Customer CustomerID="NORTS" City="London" ContactName="Simon Crowther" />

<Customer CustomerID="THEBI" City="Portland" ContactName="Liz Nixon" />

</Customers>

 

 

2. Change the CreateCustomer method in the Program class to read the data in from the XML file:

 

static IEnumerable<Customer> CreateCustomers()

{

return

from c in XDocument.Load("Customers.xml")

.Descendants("Customers").Descendants()

select new Customer

{

City = c.Attribute("City").Value,

CustomerID = c.Attribute("CustomerID").Value

};

}

 

3. Press Ctrl+F5 to build and run the application. Notice the output still only contains those customers that are located in London. Now press any key to terminate the application.

 

Notice the query remains the same. The only method that was altered was the CreateCustomers method.


Date: 2015-02-03; view: 739


<== previous page | next page ==>
Task 3 – Querying Structured Types | Task 4 – Transforming XML Output
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.007 sec.)