Blue Theme Orange Theme Green Theme Red Theme
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Home | Forums | Videos | Photos | Blogs | Beginners | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Nevron Gauge for SharePoint
Search :       Advanced Search »
Home » LINQ » Reading and Writing XML using XLinq

Reading and Writing XML using XLinq

XLinq is a new API to work with XML using LINQ. XLinq is more flexible, simple, and faster programming model to read, write, manipulate, and traverse XML documents.

Author Rank :
Page Views : 766
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Nevron Gauge for SharePoint
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


What is XLinq?

 

XLinq is the name for API to work with XML in next version of .NET with the power of LINQ. It provides functionality to read, write, and manipulate XML documents. XLinq lets you run SQL like queries on XML documents.

 

Why XLinq?

 

The answer to this question is, XLinq is redesigned from scratch to keep in mind the problems with existing technologies including Document Object Model (DOM), XmlReader, and XSLT, MSXML.  

 

XLinq is more powerful, flexible, and a single API to provide all functionality to read, write, manipulate, and traverse XML document. It provides all functionalities of DOM, XQuery, XPath, XSLT, and MSXML.

 

I will discuss XLinq features in more details in my forthcoming articles.

 

This article shows how to create an XML document using XLinq and later load it and loop through the elements of XML.

 

Note: To run this sample, you must have Visual Studio 2005 and LINQ CTP May 2006 installed on your machine. 

 

You must import following namespace:

using System.Xml.XLinq;

The XElement class represents an XML element in XLinq. The code creaes a root node called Authors and adds children Author nodes. The XAttribute class represents an attribute of an element.

 

XElement.Save method saves the contents of XElement to a XML file.

 

// Create a root node

            XElement authors = new XElement("Authors");

            // Add child nodes

            XAttribute name = new XAttribute("Author", "Mahesh Chand");

            XElement book = new XElement("Book", "GDI+ Programming");

            XElement cost = new XElement("Cost", "$49.95");

            XElement publisher = new XElement("Publisher", "Addison-Wesley");

            XElement author = new XElement("Author");

            author.Add(name);

            author.Add(book);

            author.Add(cost);

            author.Add(publisher);

            authors.Add(author);

 

            name = new XAttribute("Name", "Mike Gold");

            book = new XElement("Book", "Programmer's Guide to C#");

            cost = new XElement("Cost", "$44.95");

            publisher = new XElement("Publisher", "Microgold Publishing");

            author = new XElement("Author");

            author.Add(name);

            author.Add(book);

            author.Add(cost);

            author.Add(publisher);

            authors.Add(author);

 

            name = new XAttribute("Name", "Scott Lysle");

            book = new XElement("Book", "Custom Controls");

            cost = new XElement("Cost", "$39.95");

            publisher = new XElement("Publisher", "C# Corner");

            author = new XElement("Author");

            author.Add(name);

            author.Add(book);

            author.Add(cost);

            author.Add(publisher);

            authors.Add(author);          

 

            authors.Save(@"Authors.xml");

The output xml file looks like this:

<?xml version="1.0" encoding="utf-8" ?>

- <Authors>
- <Author Author="Mahesh Chand">
  <Book>GDI+ Programming</Book>
  <Cost>$49.95</Cost>
  <Publisher>Addison-Wesley</Publisher>
  </Author>
- <Author Name="Mike Gold">
  <Book>Programmer's Guide to C#</Book>
  <Cost>$44.95</Cost>
  <Publisher>Microgold Publishing</Publisher>
  </Author>
- <Author Name="Scott Lysle">
  <Book>Custom Controls</Book>
  <Cost>$39.95</Cost>
  <Publisher>C# Corner</Publisher>
  </Author>
  </Authors>

The following code reads the Authors.xml file and loops through authors and displays the contents.

            XElement allData = XElement.Load("Authors.xml");
            if (allData != null)
            {
                IEnumerable<XElement> authors = allData.Descendants("Author");
                foreach(XElement author in authors)
                    Console.WriteLine((string)author);
            }

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Dinesh Beniwal
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Gauge for SharePoint
Become a Sponsor
 Comments
6 Months Free & No Setup Fees ASP.NET Hosting!
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.