Customer firstnamelucy lastnameharrington

Info icon This preview shows pages 483–486. Sign up to view the full content.

View Full Document Right Arrow Icon
<Customer FirstName="Lucy" LastName="Harrington"> <EmailAddress>[email protected]</EmailAddress> </Customer> </Customers> While it’s often convenient to be able to create and add elements and attributes one step at a time, these classes offer constructors that allow us to do more work in a single step. If we know exactly what we want to put in an element, this can lead to neater looking code. For example, we can replace the foreach loop with the code in Example 12-4 . Example 12-4. Constructing an XElement all at once foreach (Customer customer in customers) { // Create new element representing the customer object. var customerElem = new XElement("Customer", new XAttribute("FirstName", customer.FirstName), new XAttribute("LastName", customer.LastName), new XElement("EmailAddress", customer.EmailAddress) ); // Finally add the customer element to the XML document rootElem.Add(customerElem); } The only difference is that we’re passing all the XAttribute and XElement objects to the containing XElement constructor, rather than passing them to Add one at a time. As well as being more compact, it’s pretty easy to see how this code relates to the structure of the XML element being produced. We can also use this technique in conjunction with LINQ. Putting the LINQ in LINQ to XML We’ve seen several examples that construct an XElement , passing the name as the first argument, and the content as the second. We’ve passed strings, child elements, and attributes, but we can also provide an implementation of IEnumerable<T> . So if we add a using System.Linq; directive to the top of our file, we could use a LINQ query as the second constructor argument as Example 12-5 shows. Creating XML Documents | 459
Image of page 483

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

View Full Document Right Arrow Icon
Example 12-5. Generating XML elements with LINQ var customerXml = new XDocument(new XElement("Customers", from customer in customers select new XElement("Customer", new XAttribute("FirstName", customer.FirstName), new XAttribute("LastName", customer.LastName), new XElement("EmailAddress", customer.EmailAddress) ))); This generates the whole of the XML document in a single statement. So the work that took 25 lines of code in Example 12-1 comes down to just seven. Example 12-6 shows the whole example, with its much simplified Main method. Example 12-6. Building XML with LINQ using System; using System.Collections.Generic; using System.Linq; using System.Xml.Linq; namespace Programming_CSharp { // Simple customer class public class Customer { // Same as in Example 12-1 } // Main program public class Tester { static void Main() { List<Customer> customers = CreateCustomerList(); var customerXml = new XDocument(new XElement("Customers", from customer in customers select new XElement("Customer", new XAttribute("FirstName", customer.FirstName), new XAttribute("LastName", customer.LastName), new XElement("EmailAddress", customer.EmailAddress) ))); Console.WriteLine(customerXml.ToString()); Console.Read(); } // Create a customer list with sample data private static List<Customer> CreateCustomerList() { List<Customer> customers = new List<Customer> { new Customer { FirstName = "Orlando", LastName = "Gee", 460 | Chapter 12: XML
Image of page 484
EmailAddress = "[email protected]"},
Image of page 485

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

View Full Document Right Arrow Icon
Image of page 486
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