NHibernate Example Code in C#.Net

In this article, i will create a simple application that which implements NHibernate, in database side, i will use SQL Server 2008. What is NHibernate ? I will not explain it in there,  there are lot of explanations in the internet, like in here.
You can download latest NHibernate version here.
First, i will create a simple database that have just one table (and i added some record in there):
This is the DDL (Data Definition Language)






create table ACCOUNT (
   ACCID                int         identity(1,1)       not null,
   USERNAME             varchar(50)                     null,
   PASS                 varchar(50)                     null,
   constraint PK_ACCOUNT primary key nonclustered (ACCID)

Hmm.. what does this Identity(1,1)  mean ??? It is mean we will set ACCID attribute, the primary key, to become autonumbered, the value will be added automatically incrementing from 1, 2, 3, … and so on.
Next step, we will create simple solution in Visual Studio 2008 SP1 (remember … to connect to SQL Server 2008, your Visual Studio 2008 must be Service Pack 1 ).

Add some references
  1. NHibernate.dll (NHibernate library)
  2. NHibernate.ByteCode.LinFu.dll (for lazy loading)
  3. System.configuration


Test your connection with database using Server Explorer


Do not forget to copy the connection string…

For my example, my database connection string is :  ]
Data Source=WIRTH;Initial Catalog=TestNHibernate;User ID=azer89;Password=azer89
Next,  create an App.config file in your project (Just Right click your project, Add, New Item, and Application Configuration File)

Set your App.config like this :
















<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
  </configSections>
  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
      <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
      <property name="connection.connection_string">Data Source=WIRTH;Initial Catalog=TestNHibernate;User ID=azer89;Password=azer89</property>
      <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
      <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
      <mapping assembly="NHibernateTest"/>
    </session-factory>
  </hibernate-configuration>
</configuration>
You must pay attention in property connection.connection_string and dialect in code above. Add a model class similar to Account Table, the fields should be similar too

Your Account.cs should be like this :































using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace NHibernateTest
{
    class Account
    {
        private int accId;
        private string username;
        private string pass;
 
        public virtual int AccId
        {
            get { return accId; }
            set { accId = value; }
        }
 
        public virtual string UserName
        {
            get { return username; }
            set { username = value; }
        }
 
        public virtual string Pass
        {
            get { return pass; }
            set { pass = value; }
        }
    }
}
After creating Account class, we will create map to Account Table using XML file, so NHibernate will know that Account class in application will map to Account Table in database. The XML filename will be “Account.nbm.xml”















<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernateTest" assembly="NHibernateTest">
  <class name="Account" table="ACCOUNT" lazy="false">
    <id name="AccId">
      <column name="ACCID"/>
      <generator class="native"/>
    </id>
    <property name="UserName">
      <column name="username"/>
    </property>
    <property name="Pass">
      <column name="pass"/>
    </property>
  </class>
</hibernate-mapping>
Look at generator class, the value is “native”, it is because we have set ACCID column to autoincrement. Oh yeah, Build Action for hbm.xml file must be “Embedded Resource”

Next step is to create persistence class, we will name it NHibernateHelper.cs
































using System.Reflection;
using NHibernate;
using NHibernate.Cfg;
 
namespace NHibernateTest
{
    public sealed class NHibernateHelper
    {
        private static ISessionFactory SessionFactory;
 
        private static void OpenSession()
        {
            Configuration configuration = new Configuration();
            configuration.AddAssembly(Assembly.GetCallingAssembly());
            SessionFactory = configuration.BuildSessionFactory();
        }
 
        public static ISession GetCurrentSession()
        {
            if (SessionFactory == null)
                NHibernateHelper.OpenSession();
 
            return SessionFactory.OpenSession();
        }
 
        public static void CloseSessionFactory()
        {
            if (SessionFactory != null)
                SessionFactory.Close();
        }
    }
}
Since ISessionFactory is expensive to be created (it almost take one second in my old computer) we will create one object of ISessionFactory, otherwise with ISession, very cheap to be created, so we can create it very often in your application. We will test the code :































using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
 
namespace NHibernateTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string username = "reza";
            string pass = "reza";
 
            ISession session = NHibernateHelper.GetCurrentSession();
            IQuery query = session.CreateQuery("FROM Account WHERE username = :name  AND pass = :pass ");
            query.SetString("name", username);
            query.SetString("pass", pass);
            IList<Account> acc = query.List<Account>();
            session.Close();
            if (acc.Count == 0)
                Console.WriteLine("Cannot find specified user");
            else
                Console.WriteLine("Found " + acc[0].UserName);
 
            Console.ReadKey();
 
        }
    }
}
It works ! Fine

Linq to SQL is Good or Bad

Linq to Sql: The Good, The Bad & The Bottom Line

I promised my take on Linq to Sql a few days ago. I have spent some time over the past couple days playing with Linq to Sql connected to the AdventureWorks SQL Server sample database.

I have a lot of experience working with NHibernate so you may see some comparisons throughout the post.

Overview

Most everyone who is likely to read this post probably knows what Linq to Sql is. For those that don't, Linq to Sql (and really Linq in general) has been one of the most talked about (Once we found out Linq to Entities wasn't going to ship with Visual Studio 2008) and hyped features of Visual Studio 2008 and the .NET 3.5 framework.

Linq to Sql is actually a big shift for Microsoft. Linq to Sql is Microsoft's first production quality Object Relational Mapper or O/RM for short. They may have tried in the past with products such as ObjectSpaces, but this is the first tool to be released as a completed tool. O/RM tools exist to try and solve the Object-relational impedance mismatch which basically says that most applications are developed in object oriented programming languages these days yet the data which they operate on is typically stored in a relational database. This process of moving data between the objects and relations and vice versa is described as the impedance mismatch. There are obviously many fundamental differences between data stored in a relation and data stored in our objects.

Traditionally Microsoft has endorsed using DataSets to solve this problem. DataSets are essentially a relation based object in your object oriented programming language. Essentially it would allow you to work with your data in your application as relational data. The problem with this? You fail to take advantage of object oriented application design and the advantages it brings to you. Typically these programs have little testability and a significant amount of duplication. As such many O/RM tools became popular (although far less so than if Microsoft had endorsed them) such as NHibernate, LLBLGen Pro, Vanatec OpenAccess, Wilson ORMapper, EntitySpaces, eXpress Persistent Objects and many others (apologies to any I didn't list).

Note that Linq to Sql isn't necessarily a direct competitor to NHibernate or the other above listed O/RM tools for the .NET framework, that is Linq to Entities (AKA ADO.NET Entity Framework). Linq to Sql is more of an introduction to the O/RM world.

The Good

  • The Linq query language itself
The Linq query language is just awesome. It really is a joy when you start to work with it. It can quickly become a pain because it is complex, but then it makes you realize just how powerful it is. I have never seen a query language that is quite so rich. Basic queries are very simple to write and understand, yet it also provides functionality for very complex queries.

Plus, the queries are strongly typed so now there is much less to worry about when refactoring your business objects as compile-time checks are now available for your queries. Note that even with stored procedures, if you change a column in a table referenced by a stored procedure, it won't inform you that you just broke a stored procedure. Likewise stored queries in your applications will not inform you if you change a property name or column either.

For fun see the following blog post: Taking LINQ to Objects to Extremes: A fully LINQified RayTracer. This is not something you woudl actually do, but it does help reinforce just how powerful Linq really is.
  • Better Naming Conventions Than NHibernate
While working with Linq to Sql I felt that the methods on the context were easy to understand and more intuitive than the NHibernate equivalents. For example, when you want to save your changes to your database NHibernate says Flush whereas Linq to Sql uses SubmitChanges. But the big advantages are Linq to Sql's InsertOnSubmit vs NHibernate's Save as well as Attach versus NHibernate's Update or Lock methods.

I can't tell you how many times I've explained how the Save, Update and Lock functionality for NHibernate works. Most people seem to think that they need to call these methods to cause a database operation to take place. They assume Save = Execute Insert NOW, and Update means execute an update NOW! Then they use Flush for good measure because someone told them too. The Linq to Sql naming convention seems to imply that that is not quite what is going on.
  • Simple to Get Started
It didn't take me very long to get up and going with Linq to Sql. While I'm not the biggest fan of the Object Relational Designer, it sure is easy to use and fast to build basic object graphs. Someone who is not familiar with O/RM tools should be able to have objects mapped to database tables in a matter of minutes. This could work very well for simple RAD applications. This process really couldn't be much simpler.
  • Superior Optimistic Concurrency Support
My apologies to any O/RM tools out there that have as good concurrency support as Linq to Sql, I just know I prefer the flexibilty offered by Linq to Sql over NHibernate's. Now, that being said NHibernate's concurrency has always worked fine for me, it's just nice to have additional options.

First, when a ChangeConflictException is thrown it includes a ton of information such as the entity involved, the columns involved and allows your code to recover from it. Linq to Sql will also let you configure if you want to catch all change conflicts or fail as soon as the first conflict is found. These are features, which to my knowledge, NHibernate does not support.

Plus, this is basic but Linq to Sql has native support for SQL Server timestamp columns. This allows you to ensure that you know of all updates even if it occurs outside the scope of Linq to Sql. For some reason NHibernate still does not support this type of column. Instead it rolls its own version column.

Resolving stale data with RefreshMode allows for many options when re-syncing your objects with the database. Again, I just like the options.
  • Superior Stored Procedure Support
If you have a wealth of stored procedures, rest assured they are easy to use from Linq to Sql. Just drag (I do feel dirty using that word) the stored procedure from the server explorer to the methods list in the object relational designer and you will see a new method on your associated context which directly calls that stored procedure. To your code it looks the same as any other method.

Note it is also possible to write your Linq to Sql CRUD through stored procedures. This is also a relatively simple process.

The Bad
  • Very Basic Object Support
This is actually the killer here. Linq to Sql is a very basic O/RM and does not support many of the object oriented concepts sophisticated applications are likely to use. Just a few of the missing features are:
    • No Inheritance
    • No Value based objects (IE NHibernate Components)
    • No Timespan support (A huge problem for the Logistics field I work in)
    • Collections limited to EntitySet (which isn't even a real Set)
      • Where is the Dictionary support at least?
  • No SaveOrUpdate Equivalent
This forces more persistence knowledge to a lower level requiring that all code which associates an object with a context must know if it already exists in the database or not. This basically just adds extra checks in your code which should not be necessary. Sometimes it can seem a bit dirty to check if an object already has a primary key or not yourself, it seems like logic which doesn't belong within the application itself.
  • GUI based Drag & Drop
Yes, I know you can use a seperate mapping file, much like you can with NHibernate, but this isn't realistic. If you don't use the designer, you don't get the code generation. If you don't get the code generation you are responsible for writing all of the many Hooks in your objects that Linq to Sql needs. Folks, these objects are quite dirty. At least with NHibernate your objects are complete persistence ignorant (aka POCO aka Plain Old CLR Object) meaning they look clean and usable for more than just NHibernate. Therefore using anything besides the designer isn't very feasible.

The big problem here though is that your entire object graph needs to live in one diagram and the code behind these objects winds up in a single code file by default. This just isn't acceptable for applications of any size. Diagrams which contain 20-30 objects would be a major pain here, let alone applications that have hundreds. For large applications this just wouldn't fly.
  • Relationships Aren't Interface Based
All of the associations to related objects are handled with EntitySet and EntityRef. Whereas with NHibernate you have the ISet and just the object type you expect. This basically forces the Linq to Sql references on your object, decreasing the ability for unit testing your objects in my opinion. I also don't like the persistence based decencies on my objects.
  • Transaction API is Goofy
For whatever reason you need to handle all explicit transactions outside of the Linq to Sql context. You have to create and the commit it outside the context while supplying the transaction to the context while it is in use. Linq to Sql implicitly uses transactions for all calls to SubmitChanges, but you would think it would be possible to begin new transactions via the context, and then commit or roll them back through the context as well.

The Bottom Line

Really, I have only touched on a brief overview of Linq to Sql here. The important question I ask myself is, "Would I use this framework?". Well, it's a bit of a difficult question. If I was writing a small application which I knew would not grow in to a large one and my object model would be simple enough for the limited object support, yes I would use it. I could get up and going very fast, and I enjoy working with the context interfaces.

However, if I was working on a larger application (really doesn't take much to be too large for what I would do with Linq to Sql), or one which I thought had potential to adjust and grow over time, I would skip Linq to Sql and look for my trusty NHibernate.

So really, it would only be used for a very small subset of problems out there that I would try to solve.

All of that being said, I think Linq to Sql is very important to the .NET development community. Microsoft has historically tried to pretend that O/RM tools didn't exist and to do any development except their DataSets or repetitive patterns was crazy. Now that Microsoft has a framework to endorse it should greatly expand the exposure to such technologies in the .NET development community. I think overall this is a good thing, and will result in overall superior developers.

My only concern with this introduction is that people may get the idea that O/RM tools are nice, and get you up and going fast but fall flat on their face once you try to do anything advanced and then you need to resort to the same tools you used all along. This was actually a very common opinion by people I talked to about NHibernate a few years ago. They had heard of others using O/RM tools (not NHibernate specifically) and how they just don't handle advanced things, they are only good for simple things.

With Linq to Sql I hope developers become exposed to O/RM and become curious about other tools such as NHibernate when Linq to Sql is too simple for what they need instead of grouping all O/RM tools together as being too simple and idealistic.

I'm actually excited about the potential of the .NET development community now that more people will be exposed to O/RM. Long live O/RM tools, you have been lifesavers for me!

Linq to XML using C# and VB.Net

A lot of developers over the past few months have requested us for tutorials focusing on LINQToXML. Although I have written a couple of them in the past, I decided to republish these tips in the form of a single post. In this article, we will explore 24 ‘How Do I’ kind of examples using LINQ to XML. I assume you are familiar with LINQ. If not, you can start off with LINQ by checking some tutorials over here and here.
 Excel file API for: XLS, XLSX, CSV and ODS
For this article, we will be using a sample file called ‘Employees.xml’ for all our samples which is available with the source code. So make sure you keep it handy with you while are practicing these examples. The mark up for Employees.xml is as follows:
<?xml version="1.0" encoding="utf-8" ?>
<Employees>
 <Employee>
    <EmpId>1</EmpId>
    <Name>Sam</Name>   
    <Sex>Male</Sex>
    <Phone Type="Home">423-555-0124</Phone>
    <Phone Type="Work">424-555-0545</Phone>
   <Address>
      <Street>7A Cox Street</Street>
      <City>Acampo</City>
      <State>CA</State>
      <Zip>95220</Zip>
      <Country>USA</Country>
    </Address>
 </Employee>
 <Employee>
    <EmpId>2</EmpId>
    <Name>Lucy</Name>
    <Sex>Female</Sex>
    <Phone Type="Home">143-555-0763</Phone>
    <Phone Type="Work">434-555-0567</Phone>
    <Address>
      <Street>Jess Bay</Street>
      <City>Alta</City>
      <State>CA</State>
      <Zip>95701</Zip>
      <Country>USA</Country>
    </Address>
 </Employee>
 <Employee>
    <EmpId>3</EmpId>
    <Name>Kate</Name>
    <Sex>Female</Sex>
    <Phone Type="Home">166-555-0231</Phone>
    <Phone Type="Work">233-555-0442</Phone>
    <Address>
      <Street>23 Boxen Street</Street>
      <City>Milford</City>
      <State>CA</State>
      <Zip>96121</Zip>
      <Country>USA</Country>
    </Address>
 </Employee>
 <Employee>
    <EmpId>4</EmpId>
    <Name>Chris</Name>
    <Sex>Male</Sex>
    <Phone Type="Home">564-555-0122</Phone>
    <Phone Type="Work">442-555-0154</Phone>
    <Address>
      <Street>124 Kutbay</Street>
      <City>Montara</City>
      <State>CA</State>
      <Zip>94037</Zip>
      <Country>USA</Country>
    </Address>
 </Employee>
</Employees>
 
The application is a console application targeting .NET 3.5 framework, although you can use the latest .NET 4.0 framework too. I have also used ‘query expressions’, instead of Lambda expression in these samples. It is just a matter of preference and you are free to use any of these.
This tutorial has been divided into 2 sections:
Section 1: Read XML and Traverse the Document using LINQ To XML
Section 2: Manipulate XML content and Persist the changes using LINQ To XML
The following namespaces are needed while testing the samples: System; System.Collections.Generic; System.Linq; System.Text; System.Xml; System.Xml.Linq;
Go grab a hot cup of coffee, put on your developer cap and let us get started:
 
Section 1: Read XML and Traverse the XML Document using LINQ To XML
 
1. How Do I Read XML using LINQ to XML
There are two ways to do so: Using the XElement class or the XDocument class. Both the classes contain the ‘Load()’ method which accepts a file, a URL or XMLReader and allows XML to be loaded. The primary difference between both the classes is that an XDocument can contain XML declaration, XML Document Type (DTD) and processing instructions. Moreover an XDocument contains one root XElement.
Using XElement
C#
XElement xelement = XElement.Load("..\\..\\Employees.xml");
IEnumerable<XElement> employees = xelement.Elements();
// Read the entire XML
foreach (var employee in employees)
{
    Console.WriteLine(employee);
}
VB.NET (Converted Code)
Dim xelement As XElement = XElement.Load("..\..\Employees.xml")
Dim employees As IEnumerable(Of XElement) = xelement.Elements()
' Read the entire XML
For Each employee In employees
      Console.WriteLine(employee)
Next employee
Output:
image_1
Using XDocument
C#
XDocument xdocument = XDocument.Load("..\\..\\Employees.xml");
IEnumerable<XElement> employees = xdocument.Elements();
foreach (var employee in employees)
{
    Console.WriteLine(employee);
}
 
VB.NET (Converted Code)
Dim xdocument As XDocument = XDocument.Load("..\..\Employees.xml")
Dim employees As IEnumerable(Of XElement) = xdocument.Elements()
For Each employee In employees
      Console.WriteLine(employee)
Next employee
Output:
image_11
Note 1: As you can observe, XDocument contains a single root element (Employees).
Note 2: In order to generate an output similar to the one using XElement, use  “xdocument.Root.Elements()” instead of  “xdocument.Elements()”
Note 3: VB.NET users can use a new feature called XML Literal.
 
2. How Do I Access a Single Element using LINQ to XML
Let us see how to access the name of all the Employees and list them over here
C#
XElement xelement = XElement.Load("..\\..\\Employees.xml");
IEnumerable<XElement> employees = xelement.Elements();
Console.WriteLine("List of all Employee Names :");
foreach (var employee in employees)
{
    Console.WriteLine(employee.Element("Name").Value);
}
 
VB.NET (Converted Code)
Dim xelement As XElement = XElement.Load("..\..\Employees.xml")
Dim employees As IEnumerable(Of XElement) = xelement.Elements()
Console.WriteLine("List of all Employee Names :")
For Each employee In employees
      Console.WriteLine(employee.Element("Name").Value)
Next employee
Output:
image_2
3. How Do I Access Multiple Elements using LINQ to XML
Let us see how to access the name of all Employees and also list the ID along with it
C#
XElement xelement = XElement.Load("..\\..\\Employees.xml");
IEnumerable<XElement> employees = xelement.Elements();
Console.WriteLine("List of all Employee Names along with their ID:");
foreach (var employee in employees)
{
    Console.WriteLine("{0} has Employee ID {1}",
        employee.Element("Name").Value,
        employee.Element("EmpId").Value);
}
VB.NET (Converted Code)
Dim xelement As XElement = XElement.Load("..\..\Employees.xml")
Dim employees As IEnumerable(Of XElement) = xelement.Elements()
Console.WriteLine("List of all Employee Names along with their ID:")
For Each employee In employees
      Console.WriteLine("{0} has Employee ID {1}", employee.Element("Name").Value, employee.Element("EmpId").Value)
Next employee
Output:
image_3
4. How Do I Access all Elements having a Specific Attribute using LINQ to XML
Let us see how to access details of all Female Employees
C#
XElement xelement = XElement.Load("..\\..\\Employees.xml");
var name = from nm in xelement.Elements("Employee")
           where (string)nm.Element("Sex") == "Female"
           select nm;
Console.WriteLine("Details of Female Employees:");
foreach (XElement xEle in name)
    Console.WriteLine(xEle);
 
VB.NET (Converted Code)
Dim xelement As XElement = XElement.Load("..\..\Employees.xml")
Dim name = _
      From nm In xelement.Elements("Employee") _
      Where CStr(nm.Element("Sex")) = "Female" _
      Select nm
Console.WriteLine("Details of Female Employees:")
For Each xEle As XElement In name
      Console.WriteLine(xEle)
Next xEle
Output:
image_4
5. How Do I access Specific Element having a Specific Attribute using LINQ to XML
Let us see how to list all the Home Phone Nos.
C#
XElement xelement = XElement.Load("..\\..\\Employees.xml");
var homePhone = from phoneno in xelement.Elements("Employee")
                where (string)phoneno.Element("Phone").Attribute("Type") == "Home"
                select phoneno;
Console.WriteLine("List HomePhone Nos.");
foreach (XElement xEle in homePhone)
{
    Console.WriteLine(xEle.Element("Phone").Value);
}
 
VB.NET (Converted Code)
Dim xelement As XElement = XElement.Load("..\..\Employees.xml")
Dim homePhone = _
      From phoneno In xelement.Elements("Employee") _
      Where CStr(phoneno.Element("Phone").Attribute("Type")) = "Home" _
      Select phoneno
Console.WriteLine("List HomePhone Nos.")
For Each xEle As XElement In homePhone
      Console.WriteLine(xEle.Element("Phone").Value)
Next xEle
Output:
image_5
6. How Do I Find an Element within another Element using LINQ to XML
Let us see how to find the details of Employees living in 'Alta' City
C#
XElement xelement = XElement.Load("..\\..\\Employees.xml");
var addresses = from address in xelement.Elements("Employee")
                where (string)address.Element("Address").Element("City") == "Alta"
               select address;
Console.WriteLine("Details of Employees living in Alta City");
foreach (XElement xEle in addresses)
    Console.WriteLine(xEle);
 
VB.NET (Converted Code)
Dim xelement As XElement = XElement.Load("..\..\Employees.xml")
Dim addresses = _
      From address In xelement.Elements("Employee") _
      Where CStr(address.Element("Address").Element("City")) = "Alta" _
      Select address
Console.WriteLine("Details of Employees living in Alta City")
For Each xEle As XElement In addresses
      Console.WriteLine(xEle)
Next xEle
Output:
image_26
7. How Do I Find Nested Elements (using Descendants Axis) using LINQ to XML
Let us see how to list all the zip codes in the XML file
C#
XElement xelement = XElement.Load("..\\..\\Employees.xml");
Console.WriteLine("List of all Zip Codes");
foreach (XElement xEle in xelement.Descendants("Zip"))
{
    Console.WriteLine((string)xEle);
}
 
VB.NET (Converted Code)
Dim xelement As XElement = XElement.Load("..\..\Employees.xml")
Console.WriteLine("List of all Zip Codes")
For Each xEle As XElement In xelement.Descendants("Zip")
      Console.WriteLine(CStr(xEle))
Next xEle
Output:
image_37
8. How do I apply Sorting on Elements using LINQ to XML
Let us see how to List and Sort all Zip Codes in ascending order
C#
XElement xelement = XElement.Load("..\\..\\Employees.xml");
IEnumerable<string> codes = from code in xelement.Elements("Employee")
                            let zip = (string)code.Element("Address").Element("Zip")
                            orderby zip
                            select zip;
Console.WriteLine("List and Sort all Zip Codes");
 
foreach (string zp in codes)
    Console.WriteLine(zp);
 
VB.NET (Converted Code)
Dim xelement As XElement = XElement.Load("..\..\Employees.xml")
Dim codes As IEnumerable(Of String) = _
      From code In xelement.Elements("Employee") _
      Let zip = CStr(code.Element("Address").Element("Zip")) _
      Order By zip _
      Select zip
Console.WriteLine("List and Sort all Zip Codes")
 
For Each zp As String In codes
      Console.WriteLine(zp)
Next zp
Output:
image_48
 
Section 2: Manipulate XML content and Persist the changes using LINQ To XML
 
9. Create an XML Document with Xml Declaration/Namespace/Comments using LINQ to XML
When you need to create an XML document containing XML declaration, XML Document Type (DTD) and processing instructions, Comments, Namespaces, you should go in for the XDocument class.
C#
XNamespace empNM = "urn:lst-emp:emp";
 
XDocument xDoc = new XDocument(
            new XDeclaration("1.0", "UTF-16", null),
            new XElement(empNM + "Employees",
                new XElement("Employee",
                    new XComment("Only 3 elements for demo purposes"),
                    new XElement("EmpId", "5"),
                    new XElement("Name", "Kimmy"),
                    new XElement("Sex", "Female")
                    )));
 
StringWriter sw = new StringWriter();
xDoc.Save(sw);
Console.WriteLine(sw);
VB.NET (Converted Code)
        Dim empNM As XNamespace = "urn:lst-emp:emp"
 
        Dim xDoc As New XDocument(New XDeclaration("1.0", "UTF-16", Nothing), _
                    New XElement(empNM + "Employees", _
                    New XElement("Employee", _
                    New XComment("Only 3 elements for demo purposes"), _
                    New XElement("EmpId", "5"), _
                    New XElement("Name", "Kimmy"), _
                    New XElement("Sex", "Female"))))
 
        Dim sw As New StringWriter()
        xDoc.Save(sw)
        Console.WriteLine(sw)
image_59
10. Save the XML Document to a XMLWriter or to the disk using LINQ to XML
Use the following code to save the XML to a XMLWriter or to your physical disk
C#
XNamespace empNM = "urn:lst-emp:emp";
 
XDocument xDoc = new XDocument(
            new XDeclaration("1.0", "UTF-16", null),
            new XElement(empNM + "Employees",
                new XElement("Employee",
                    new XComment("Only 3 elements for demo purposes"),
                    new XElement("EmpId", "5"),
                    new XElement("Name", "Kimmy"),
                    new XElement("Sex", "Female")
                    )));
 
StringWriter sw = new StringWriter();
XmlWriter xWrite = XmlWriter.Create(sw);
xDoc.Save(xWrite);
xWrite.Close();
 
// Save to Disk
xDoc.Save("C:\\Something.xml");
Console.WriteLine("Saved");
VB.NET (Converted Code)
       Dim empNM As XNamespace = "urn:lst-emp:emp"
 
Dim xDoc As New XDocument(New XDeclaration("1.0", "UTF-16", Nothing),_
        New XElement(empNM + "Employees", _
        New XElement("Employee", _
        New XComment("Only 3 elements for demo purposes"), _
       New XElement("EmpId", "5"), _
        New XElement("Name", "Kimmy"), _
        New XElement("Sex", "Female"))))
 
        Dim sw As New StringWriter()
        Dim xWrite As XmlWriter = XmlWriter.Create(sw)
        xDoc.Save(xWrite)
        xWrite.Close()
 
        ' Save to Disk
        xDoc.Save("C:\Something.xml")
        Console.WriteLine("Saved")
 
11. Load an XML Document using XML Reader using LINQ to XML
Use the following code to load the XML Document into an XML Reader
C#
XmlReader xRead = XmlReader.Create(@"..\\..\\Employees.xml");
XElement xEle = XElement.Load(xRead);
Console.WriteLine(xEle);
xRead.Close();
VB.NET (Converted Code)
Dim xRead As XmlReader = XmlReader.Create("..\\..\\Employees.xml")
Dim xEle As XElement = XElement.Load(xRead)
Console.WriteLine(xEle)
xRead.Close()
image_110
 
12. Find Element at a Specific Position using LINQ to XML
Find the 2nd Employee Element
C#
// Using XElement
Console.WriteLine("Using XElement");
XElement xEle = XElement.Load("..\\..\\Employees.xml");
var emp1 = xEle.Descendants("Employee").ElementAt(1);
Console.WriteLine(emp);
 
Console.WriteLine("------------");
 
//// Using XDocument
Console.WriteLine("Using XDocument");
XDocument xDoc = XDocument.Load("..\\..\\Employees.xml");
var emp1 = xDoc.Descendants("Employee").ElementAt(1);
Console.WriteLine(emp);
VB.NET (Converted Code)
' Using XElement
Console.WriteLine("Using XElement")
Dim xEle As XElement = XElement.Load("..\..\Employees.xml")
Dim emp1 = xEle.Descendants("Employee").ElementAt(1)
Console.WriteLine(emp)
 
Console.WriteLine("------------")
 
'// Using XDocument
Console.WriteLine("Using XDocument")
Dim xDoc As XDocument = XDocument.Load("..\..\Employees.xml")
Dim emp1 = xDoc.Descendants("Employee").ElementAt(1)
Console.WriteLine(emp)
image_211
13. List the First 2 Elements using LINQ to XML
List the details of the first 2 Employees
C#
XElement xEle = XElement.Load("..\\..\\Employees.xml");
var emps = xEle.Descendants("Employee").Take(2);
foreach (var emp in emps)
    Console.WriteLine(emp);
VB.NET (Converted Code)
Dim xEle As XElement = XElement.Load("..\..\Employees.xml")
Dim emps = xEle.Descendants("Employee").Take(2)
For Each emp In emps
      Console.WriteLine(emp)
Next emp
image_312
14. List the 2nd and 3rd Element using LINQ to XML
List the 2nd and 3rd Employees
C#
XElement xEle = XElement.Load("..\\..\\Employees.xml");
var emps = xEle.Descendants("Employee").Skip(1).Take(2);
foreach (var emp in emps)
    Console.WriteLine(emp);
VB.NET (Converted Code)
Dim xEle As XElement = XElement.Load("..\..\Employees.xml")
Dim emps = xEle.Descendants("Employee").Skip(1).Take(2)
For Each emp In emps
      Console.WriteLine(emp)
Next emp
image_413
15. List the Last 2 Elements using LINQ To XML
We have been posting the entire elements as output in our previous examples. Let us say that you want to display only the Employee Name, use this query:
C#
XElement xEle = XElement.Load("..\\..\\Employees.xml");
var emps = xEle.Descendants("Employee").Reverse().Take(2);
foreach (var emp in emps)
    Console.WriteLine(emp.Element("EmpId") + "" + emp.Element("Name"));
VB.NET (Converted Code)
Dim xEle As XElement = XElement.Load("..\..\Employees.xml")
Dim emps = xEle.Descendants("Employee").Reverse().Take(2)
For Each emp In emps
      Console.WriteLine(emp.Element("EmpId") + emp.Element("Name"))
Next emp
image_514
To display only the values without the XML tags, use the ‘Value’ property
C#
XElement xEle = XElement.Load("..\\..\\Employees.xml");
var emps = xEle.Descendants("Employee").Reverse().Take(2);
foreach (var emp in emps)
    Console.WriteLine(emp.Element("EmpId").Value + ". " + emp.Element("Name").Value);
 
VB.NET (Converted Code)
Dim xEle As XElement = XElement.Load("..\..\Employees.xml")
Dim emps = xEle.Descendants("Employee").Reverse().Take(2)
For Each emp In emps
      Console.WriteLine(emp.Element("EmpId").Value & ". " & emp.Element("Name").Value)
Next emp
image_115
If you notice, the results are not ordered i.e. the Employee 4 is printed before 3. To order the results, just add call Reverse() again while filtering as shown below:
C#
XElement xEle = XElement.Load("..\\..\\Employees.xml");
var emps = xEle.Descendants("Employee").Reverse().Take(2).Reverse();
foreach (var emp in emps)
    Console.WriteLine(emp.Element("EmpId").Value + ". " + emp.Element("Name").Value);
VB.NET (Converted Code)
Dim xEle As XElement = XElement.Load("..\..\Employees.xml")
Dim emps = xEle.Descendants("Employee").Reverse().Take(2).Reverse()
For Each emp In emps
      Console.WriteLine(emp.Element("EmpId").Value & ". " & emp.Element("Name").Value)
Next emp
image_216
16. Find the Element Count based on a condition using LINQ to XML
Count the number of Employees living in the state CA
C#
XElement xelement = XElement.Load("..\\..\\Employees.xml");
var stCnt = from address in xelement.Elements("Employee")
            where (string)address.Element("Address").Element("State") == "CA"
            select address;
Console.WriteLine("No of Employees living in CA State are {0}", stCnt.Count());
VB.NET (Converted Code)
XElement xelement = XElement.Load("..\\..\\Employees.xml");
var stCnt = from address in xelement.Elements("Employee")
            where (string)address.Element("Address").Element("State") == "CA"
            select address;
Console.WriteLine("No of Employees living in CA State are {0}", stCnt.Count());
image_317
17. Add a new Element at runtime using LINQ to XML
You can add a new Element to an XML document at runtime by using the Add() method of XElement. The new Element gets added as the last element of the XML document.
C#
 XElement xEle = XElement.Load("..\\..\\Employees.xml");
 xEle.Add(new XElement("Employee",
     new XElement("EmpId", 5),
     new XElement("Name", "George")));
 
 Console.Write(xEle);
VB.NET (Converted Code)
Dim xEle As XElement = XElement.Load("..\..\Employees.xml")
        xEle.Add(New XElement("Employee", _
                              New XElement("EmpId", 5), _
                              New XElement("Name", "George")))
 
Console.Write(xEle)
image_418
18. Add a new Element as the First Child using LINQ to XML
In the previous example, by default the new Element gets added to the end of the XML document. If you want to add the Element as the First Child, use the ‘AddFirst()’ method
C#
XElement xEle = XElement.Load("..\\..\\Employees.xml");
    xEle.AddFirst(new XElement("Employee",
        new XElement("EmpId", 5),
        new XElement("Name", "George")));
 
    Console.Write(xEle);
VB.NET (Converted Code)
Dim xEle As XElement = XElement.Load("..\..\Employees.xml")
   xEle.AddFirst(New XElement("Employee", _
      New XElement("EmpId", 5), _
      New XElement("Name", "George")))
 
Console.Write(xEle)
 
image_518
19. Add an attribute to an Element using LINQ to XML
To add an attribute to an Element, use the following code:
C#
XElement xEle = XElement.Load("..\\..\\Employees.xml");
    xEle.Add(new XElement("Employee",
        new XElement("EmpId", 5),
        new XElement("Phone", "423-555-4224", new XAttribute("Type", "Home"))));
 
    Console.Write(xEle);
VB.NET (Converted Code)
Dim xEle As XElement = XElement.Load("..\..\Employees.xml")
        xEle.Add(New XElement("Employee", _
                              New XElement("EmpId", 5), _
                              New XElement("Phone", "423-555-4224", _
                                           New XAttribute("Type", "Home"))))
 
        Console.Write(xEle)
image_119
20. Replace Contents of an Element/Elements using LINQ to XML
Let us say that in the XML file, you want to change the Country from “USA” to “United States of America” for all the Elements. Here’s how to do so:
C#
XElement xEle = XElement.Load("..\\..\\Employees.xml");
    var countries = xEle.Elements("Employee").Elements("Address").Elements("Country").ToList();
    foreach (XElement cEle in countries)
        cEle.ReplaceNodes("United States Of America");
 
    Console.Write(xEle);
VB.NET (Converted Code)
Dim xEle As XElement = XElement.Load("..\..\Employees.xml")
        Dim countries = xEle.Elements("Employee").Elements("Address").Elements("Country").ToList()
        For Each cEle As XElement In countries
            cEle.ReplaceNodes("United States Of America")
        Next cEle
 
Console.Write(xEle)
 
image_220
21. Remove an attribute from all the Elements using LINQ to XML
Let us say if you want to remove the Type attribute ( <Phone Type=”Home”>) attribute for all the elements, then here’s how to do it.
C#
XElement xEle = XElement.Load("..\\..\\Employees.xml");
    var phone = xEle.Elements("Employee").Elements("Phone").ToList();
    foreach (XElement pEle in phone)
        pEle.RemoveAttributes();
 
    Console.Write(xEle);
VB.NET (Converted Code)
Dim xEle As XElement = XElement.Load("..\..\Employees.xml")
        Dim phone = xEle.Elements("Employee").Elements("Phone").ToList()
        For Each pEle As XElement In phone
            pEle.RemoveAttributes()
        Next pEle
 
Console.Write(xEle)
image_321
To remove attribute of one Element based on a condition, traverse to that Element and SetAttributeValue("Type", null); You can also use SetAttributeValue(XName,object) to update an attribute value.
 
22. Delete an Element based on a condition using LINQ to XML
If you want to delete an entire element based on a condition, here’s how to do it. We are deleting the entire Address Element
C#
XElement xEle = XElement.Load("..\\..\\Employees.xml");
    var addr = xEle.Elements("Employee").ToList();
    foreach (XElement addEle in addr)
        addEle.SetElementValue("Address", null);
 
    Console.Write(xEle);
VB.NET (Converted Code)
Dim xEle As XElement = XElement.Load("..\..\Employees.xml")
    Dim addr = xEle.Elements("Employee").ToList()
        For Each addEle As XElement In addr
            addEle.SetElementValue("Address", Nothing)
        Next addEle
 
Console.Write(xEle)
 
image_422
SetElementValue() can also be used to Update the content of an Element.
 
23. Remove ‘n’ number of Elements using LINQ to XML
If you have a requirement where you have to remove ‘n’ number of Elements; For E.g. To remove the last 2 Elements, then here’s how to do it
C#
XElement xEle = XElement.Load("..\\..\\Employees.xml");
    var emps = xEle.Descendants("Employee");
    emps.Reverse().Take(2).Remove();
 
    Console.Write(xEle);
VB.NET (Converted Code)
       Dim xEle As XElement = XElement.Load("..\..\Employees.xml")
        Dim emps = xEle.Descendants("Employee")
        emps.Reverse().Take(2).Remove()
 
        Console.Write(xEle)
image_523
24. Save/Persists Changes to the XML using LINQ to XML
All the manipulations we have done so far were in the memory and were not persisted in the XML file. If you have been wondering how to persist changes to the XML, once it is modified, then here’s how to do so. It’s quite simple. You just need to call the Save() method. It’s also worth observing that the structure of the code shown below is similar to the structure of the end result (XML document). That’s one of the benefits of LINQ to XML, that it makes life easier for developers by making it so easy to create and structure XML documents.
C#
    XElement xEle = XElement.Load("..\\..\\Employees.xml");
    xEle.Add(new XElement("Employee",
    new XElement("EmpId", 5),
    new XElement("Name", "George"),
    new XElement("Sex", "Male"),
    new XElement("Phone", "423-555-4224", new XAttribute("Type", "Home")),
    new XElement("Phone", "424-555-0545", new XAttribute("Type", "Work")),
    new XElement("Address",
        new XElement("Street", "Fred Park, East Bay"),
        new XElement("City", "Acampo"),
        new XElement("State", "CA"),
        new XElement("Zip", "95220"),
        new XElement("Country", "USA"))));
 
    xEle.Save("..\\..\\Employees.xml");
    Console.WriteLine(xEle);
 
    Console.ReadLine();           
VB.NET (Converted Code)
Dim xEle As XElement = XElement.Load("..\..\Employees.xml")
        xEle.Add(New XElement("Employee", _
             New XElement("EmpId", 5), _
             New XElement("Name", "George"), _
             New XElement("Sex", "Male"), _
             New XElement("Phone", "423-555-4224", _
                 New XAttribute("Type", "Home")), _
                 New XElement("Phone", "424-555-0545", _
                   New XAttribute("Type", "Work")), _
                   New XElement("Address", _
                        New XElement("Street", "Fred Park, East Bay"), _
                        New XElement("City", "Acampo"), _
                        New XElement("State", "CA"), _
                        New XElement("Zip", "95220"), _
                        New XElement("Country", "USA"))))
 
        xEle.Save("..\..\Employees.xml")
        Console.WriteLine(xEle)
 
        Console.ReadLine()