Lastname baumgardner emailaddress jbaumgardnerfoocom

Info icon This preview shows pages 487–490. Sign up to view the full content.

View Full Document Right Arrow Icon
LastName = "Baumgardner", EmailAddress = "[email protected]"} }; return customers; } static void Main() { XDocument customerXml = CreateCustomerListXml(); Console.WriteLine("Search for single element..."); var query = from customer in customerXml.Element("Customers").Elements("Customer") where customer.Attribute("FirstName").Value == "Douglas" select customer; XElement oneCustomer = query.SingleOrDefault(); if (oneCustomer != null) { Console.WriteLine(oneCustomer); } else { Console.WriteLine("Not found"); } Console.WriteLine("\nSearch using descendant axis... "); query = from customer in customerXml.Descendants("Customer") where customer.Attribute("FirstName").Value == "Douglas" select customer; oneCustomer = query.SingleOrDefault(); if (oneCustomer != null) { Console.WriteLine(oneCustomer); } else { Console.WriteLine("Not found"); } Console.WriteLine("\nSearch using element values... "); query = from emailAddress in customerXml.Descendants("EmailAddress") where emailAddress.Value == "[email protected]" Searching in XML with LINQ | 463
Image of page 487

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
select emailAddress; XElement oneEmail = query.SingleOrDefault(); if (oneEmail != null) { Console.WriteLine(oneEmail); } else { Console.WriteLine("Not found"); } Console.WriteLine("\nSearch using child element values... "); query = from customer in customerXml.Descendants("Customer") where customer.Element("EmailAddress").Value == "[email protected]" select customer; oneCustomer = query.SingleOrDefault(); if (oneCustomer != null) { Console.WriteLine(oneCustomer); } else { Console.WriteLine("Not found"); } } // end main } // end class } // end namespace Output: Search for single element... <Customer FirstName="Douglas" LastName="Adams"> <EmailAddress>[email protected]</EmailAddress> </Customer> Search using descendant axis... <Customer FirstName="Douglas" LastName="Adams"> <EmailAddress>[email protected]</EmailAddress> </Customer> Search using element values... <EmailAddress>[email protected]</EmailAddress> Search using child element values... <Customer FirstName="Douglas" LastName="Adams"> <EmailAddress>[email protected]</EmailAddress> </Customer> This example refactors Example 12-3 by extracting the creation of the sample customer list XML document into the CreateCustomerListXml() method. You can now simply call this function in the Main() function to create the XML document. 464 | Chapter 12: XML
Image of page 488
Searching for a Single Node The first search in Example 12-7 is to find a customer whose first name is “Douglas”: var query = from customer in customerXml.Element("Customers").Elements("Customer") where customer.Attribute("FirstName").Value == "Douglas" select customer; XElement oneCustomer = query.SingleOrDefault(); if (oneCustomer != null) { Console.WriteLine(oneCustomer); } else { Console.WriteLine("Not found"); } In general, you will have some ideas about the structure of XML documents you are going to process; otherwise, it will be difficult to find the information you want. Here we know the node we are looking for sits just one level below the root element. So the source of the LINQ query—the part after the in keyword—fetches the root Customers element using the singular Element method, and then asks for all of its chil- dren called Customers by using the plural Elements method: from customer in customerXml.Element("Customers").Elements("Customer")
Image of page 489

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 490
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern