LINQ to XML Quick Brain Dump

LINQ to XML is a simple toolset that allows developers to easily interact with XML, whether it be in file form of on the wire in string form.

Write to an XML file

There are a few methods within the System.Xml.Linq namespace which make this interaction a breeze. The four main ones are:-

  • XDocument – Method which creates the XML document
  • XDeclaration – Allows you to set the declaration at the top of the file.
  • XElement – Creates a hierarchical element structure
  • XAttribute – Creates any attributes related to an element.

The following code shows a very simple way to generate an XML document for members of a club.

   1: Member[] members = new[] {
   2:  new Member{Age = 33, FullName = "John Havers", Title = "Mr"},
   3:  new Member{Age = 28, FullName = "Robin Southgate", Title = "Mrs"},
   4:  new Member{Age = 45, FullName = "Paul Chatwell", Title = "Mr"},
   5:     };
   6:  
   7: XDocument doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
   8:           new XElement("members",
   9:           from member in members
  10:           select new XElement("member",
  11:                  new XAttribute("age", member.Age),
  12:                  new XAttribute("title", member.Title),
  13:                  new XElement("fullname", member.FullName)
  14:         )
  15:     )
  16: );
  17:  
  18:         doc.Save(@"c:\members.xml");

This would generate this XML file.

   1: <?xml version="1.0" encoding="utf-8" standalone="yes" ?>
   2:     <members>
   3:      <member age="33" title="Mr">
   4:       <fullname>John Havers</fullname>
   5:      </member>
   6:      <member age="28" title="Mrs">
   7:       <fullname>Robin Southgate</fullname>
   8:      </member>
   9:      <member age="45" title="Mr">
  10:       <fullname>Paul Chatwell</fullname>
  11:      </member>
  12:     </members>

Reading from an XML file

Firstly use the Load method of the XDocument class, then a simple LINQ query will return all elements within the root element like this.

   1: IEnumerable<XElement> q = from c in loaded.Root.Elements("member")
   2:                                   select c;
   3:  
   4:         foreach (XElement element in q)
   5:         {
   6:             foreach (XElement r in element.Elements())
   7:             {
   8:                 Console.WriteLine(r.Value);
   9:             }
  10:         }

This query lists all the elements called ‘member’ within the root. Then for each member element it will again for each through for any elements within those. In our example the members full name is located there, so a simple value output will write the data to the console.

You can still use var for the query, but during running of the code it is possible to determine the resultant type.

debug

This shows that it is of type  XElement and that it is enumerable so you can change var to IENumerable<XElement> to give the code more meaning.

1 Comment

Comments have been disabled for this content.