You know you're a geek when it's not even 7AM but you've already spent half the morning reading a whitepaper about Microsoft's plans to integrate XML and relational query language functionality into the .NET Framework with Linq.  C# 3.0 is going to be hot.

Like it's forefather X# Xen Cω, XLinq does an amazing job of integrating XML directly into the Common Language Runtime and the C#/VB.NET programming languages. Below are some code samples to whet your appetite until I can get around to writing an article later this year

  1. Creating an XML document

    XDocument contactsDoc = 
        
    new XDocument(
         
    new XDeclaration("1.0", "UTF-8", "yes"),
         
    new XComment("XLinq Contacts XML Example"),
         
    new XProcessingInstruction("MyApp", "123-44-4444"),
            
    new XElement("contacts",
             
    new XElement("contact",
               
    new XElement("name","Patrick Hines"),                                       
                
    new XElement("phone", "206-555-0144"),
                
    new XElement("address",
                
    new XElement("street1", "123 Main St"),
                 
    new XElement("city", "Mercer Island"),
                 
    new XElement("state", "WA"),
                 
    new XElement("postal", "68042")
                            )
                          )
                        )
                      );

  2. Creating an XML element in the "http://example.com" namespace

    XElement contacts = new XElement("{http://example.com}contacts");

  3. Loading an XML element from a file

    XElement contactsFromFile = XElement.Load(@"c:\myContactList.xml");

  4. Writing out an array of Person objects as an XML file

    class Person {
            public string Name;
            public string[] PhoneNumbers;
    }

    var persons = new [] { new Person
                                                                {Name=
    "Patrick Hines"
    ,
                                   PhoneNumbers =
    new string
    []
                                                                                        {
    "206-555-0144", "425-555-0145"
    }
                                   },
                          
    new Person {Name="Gretchen Rivas"
    ,
                                       PhoneNumbers =
    new string
    []
                                                                                        {
    "206-555-0163"
    }
                                   }
                          };

    XElement contacts = new XElement("contacts",
                           
    from p in persons
                           
    select new XElement("contact"
    ,
                               
    new XElement("name"
    , p.Name),
                               
    from ph in
    p.PhoneNumbers
                               
    select new XElement("phone"
    , ph)
                            )

                        );

    Console.WriteLine(contacts);

  5. Print out all the element nodes that are children of the <contact> element

    foreach (x in contact.Elements()) {
               
    Console.WriteLine(x);
    }

  6. Print all the <phone> elements that are children of the <contact> element

    foreach (x in contact.Elements("phone")) {
               
    Console
    .WriteLine(x);
    }

  7. Adding a <phone> element as a child of the <contact> element

    XElement mobilePhone = new XElement("phone", "206-555-0168");
    contact.Add(mobilePhone);

  8. Adding a <phone> element as a sibling of another <phone> element

    XElement mobilePhone = new XElement("phone", "206-555-0168");
    XElement firstPhone = contact.Element("phone"
    );
    firstPhone.AddAfterThis(mobilePhone);

  9. Adding an <address> element as a child of the <contact> element

    contact.Add(new XElement("address",
                  
    new XElement("street", "123 Main St"
    ),
                  
    new XElement("city", "Mercer Island"
    ),
                  
    new XElement("state", "WA"
    ),
                  
    new XElement("country", "USA"
    ),
                  
    new XElement("postalCode", "68042"
    )
                ));

  10. Deleting all <phone> elements under a <contact> element

    contact.Elements("phone").Remove();

  11. Delete all children of the <address> element which is a child of the <contact> element

    contacts.Element("contact").Element("address").RemoveContent();

  12. Replacing the content of the <phone> element under a <contact> element

    contact.Element("phone").ReplaceContent("425-555-0155");

  13. Alternate technique for replacing the content of the <phone> element under a <contact> element

    contact.SetElement("phone", "425-555-0155");

  14. Creating a contact element with attributes multiple phone number types

    XElement contact =
         
    new XElement("contact"
    ,
               
    new XElement("name", "Patrick Hines"
    ),
               
    new XElement("phone"
    ,
                     
    new XAttribute("type", "home")
    ,
                     
    "206-555-0144"
               
    ),
               
    new XElement("phone"
    ,
                     
    new XAttribute("type", "work")
    ,
                     
    "425-555-0145"
               
    )
          );

  15. Printing the value of the <phone> element whose type attribute has the value "home"

    foreach (p in contact.Elements("phone")) {
               
    if ((string)p.Attribute("type") == "home"
    )
                   
    Console.Write("Home phone is: " + (string
    )p);
       }

  16. Deleting the type attribute of the first <phone> element under the <contact> element

    contact.Elements("phone").First().Attribute("type").Remove();

  17. Transforming our original <contacts> element to a new <contacts> element containing a list of <contact> elements whose children are <name> and <phoneNumbers>

    new XElement("contacts",
         
    from c in contacts.Elements("contact"
    )
         
    select new XElement("contact"
    ,
                c.Element(
    "name"
    ),
               
    new XElement("phoneNumbers", c.Elements("phone"
    ))
          )
    );

  18. Retrieving the names of all the contacts from Washington, sorted alphabetically 

    from    c in contacts.Elements("contact")
    where   (
    string) c.Element("address").Element("state") ==
    "WA"
    orderby (string) c.Element("name"
    )
    select  (
    string) c.Element("name");

All examples were taken from the XLinq: .NET Language Integrated Query for XML Data  white paper.