August 2007 - Posts
LINQ and XML can be used to construct, write and read XML in any .NET language. It simplifies XML reading and writing WITHOUT using XPATH or XSLT. Don't get me wrong, this does not replace legacy functions from the XML class library, merely overlaps the functionality and eases development. One aspect of LINQ to XML is that it supports writing Query Expressions and can be combined with any of the other LINQ technologies to create or use XML data as a source or destination format.
Creating a new XML file with LINQ, some key elements and required:
XDocument
XElement
XAttribute
We can build an XML document using the following syntax
XElement xml = new XElement("Albums",
new XElement("Album",
new XAttribute("albumId", "2"),
new XElement("artistName", "Dave Matthews"),
new XElement("yearReleased", "2000")
),
new XElement("Album",
new XAttribute("albumId", "3"),
new XElement("artistName", "John Mayer"),
new XElement("yearReleased", "2003")
)
);
Console.WriteLine(xml);
Which in turn will produce:
<Albums>
<Album albumId="2">
<artistName>Dave Mathews</artistName>
<yearReleased>2000</yearReleased>
</Album>
<Album albumId="3">
<artistName>John Meyer</artistName>
<yearReleased>2003</yearReleased>
</Album>
</Albums>
Later we will dscuss how we can read and query against XML data.
Now available on MSDN, there are 101 LINQ examples. This showcases how simple it can be to integrate LINQ within your future application.
The List is Broken down into separate operations, however its all you need to get up and running with LINQ and ORCAS.
Check it out here: 101 LINQ Samples
The LINQ Project is fast becoming one of the most anticipated integrated technologies in Visual Studio 2008 (ORCAS). LINQ is simply a set of extensions to the .NET framework that encompass language-integrated query, set, and transform operations. It extends C# and Visual Basic with native language syntax for queries and provides class libraries to take advantage of these capabilities.
Microsoft have seen that the next big challenge in programming technology is to reduce the complexity of accessing and integrating information that is not natively defined using OO technology. The two most common sources of non-OO information are relational databases and XML.
Put simply, the new extensions, allow you to easily perform database type queries directly in code! Standard query operators allow queries to be applied to any IEnumerable<T> based information source.
Here are some sample snippets, taken straight from Microsoft's web site...
using System;
using System.Linq;
using System.Collections.Generic;
class app
{
static void Main()
{
string[] names = { "Burke", "Connor", "Frank",
"Everett", "Albert", "George",
"Harris", "David" };
IEnumerable<string> query = from s in names
where s.Length == 5
orderby s
select s.ToUpper();
foreach (string item in query) Console.WriteLine(item);
}
}
As you can see in this very simple snippet of code, the idea is to mimic syntax of T-SQL, and if it is as good as it looks so far we may have a lot more to say on it....
I'm sure there will be more to come!
The March CTP release of ORCAS (Visual Studio 2008) incorporates a number of new and exciting technologies, as well as enhancing some of the older greats. The current release of the IDE implements tools and functionality supporting LINQ (Microsoft's .NET Extensions, Language Integration Query), AJAX, Windows Presentation Foundation (codenamed CIDER), Windows Communication Foundation, Windows Workflow Foundation and the .NET framework 3.5 (downloaded separately). According to Microsoft the latest framework will support applications built in .NET 2.0 and 3.0. Further to this raft of newly integrated features, it also extends role-based and Team System collaboration features.
Traditionally, Microsoft offers a Go Live license at beta 2, and that will almost certainly be the case here. Microsoft have saturated the market with new capabilities, and many of these platforms and technologies represent the culmination of years of work.
More on LINQ and ORCAS in coming posts...