Importing/exporting XML data using LINQ

In my previous post, you saw how to use LINQ to SQL. We'll move on with LINQ and create a small import/export application using LINQ to XML. LINQ to XML enables you to query from and write to an XML structure. It is much more efficient than XmlReader because it lets you use all capabilities of a querying language over XML structure. It uses new XML objects such as XDocument, XElement and other. You'll see it soon.

XML format is often used to transfer data from one application or system to another. To accomplish the transfer, we need to create an export utility that will for example load data from a database, format it in XML format so that it can be readble by another application, and we have to create an import utility that will parse XML and for example store data in database. We can use LINQ to SQL and LINQ to XML to accomplish this in a very simple manner.

Let's say we have two applications that can communicate with each other through XML. The data that travels between those two applications looks like this:

<Contacts>
    <Contact>
       
<
FirstName>Petar</FirstName>
       
<
LastName>Petrovic</LastName>
        <
Email>p.petar@someemail.com</Email>
        <
Address>Pere Perica 10</Address>
        <
ZipCode>1000</ZipCode>
        <
City>Belgrade</City>
        <
State>Serbia</State>
    </
Contact>
</
Contacts>

The first application gets the data from the database and saves it to XML file. In the code below we use ContactDataContext to query the database, but instead of retrieving the data in IEnumarable<T> variable, we are retreiving the data in XElement object. XElement represents an XML element that is part of XDocument class - which represents the entire XML document. When we retreive the data it is actually structured XML data. We just have to save it using the Save method. In the example below, we'll export just FirstName, LastName and Email.

ContactDataContext db = new ContactDataContext();

XElement xmlDoc = new XElement("Contacts",
                  from c in
db.Contacts
                 
orderby c.ContactID
                 
select new XElement("Contact",
                        
new XElement("ContactID", c.ContactID),
                        
new XElement("FirstName", c.FirstName),
                        
new XElement("LastName", c.LastName)));

xmlDoc.Save(Server.MapPath(@"~/export.xml"));

The second application has to load the data from XML and store it in the database. In the example below, we use XDocument class to load the XML file, and recognizable LINQ syntax to query the data from it. But, instead of retrieveing it in XElement class, we'll retrieve the data in IEnumarable<Contact> collection. That way we can easily insert retrieved data into the database using ContactDataContext class.

XDocument xmlDoc = XDocument.Load(Server.MapPath(@"~/import.xml"));

var contacts = from c in xmlDoc.Descendants("Contact")
              
select new Contact
               
{
                   FirstName = c.Element("FirstName").Value,
                   LastName = c.Element(
"LastName").Value,
                   Email = c.Element(
"Email").Value
               
};

ContactDataContext db = new ContactDataContext();

db.Contacts.InsertAllOnSubmit(contacts);
db.SubmitChanges();

Summary

You saw how to query data from XML files and how to manipulate XML data in the code. For better understaning of new classes for XML support read this MSDN article.

For more examples on LINQ to XML read following articles:

 

Published Thursday, January 31, 2008 11:30 AM by janko
Filed under: , ,
Powered by Community Server (Commercial Edition), by Telligent Systems