Tuesday, September 06, 2011

How to read XML using LINQ

LINQ is 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.
One of the big programming model improvements being made in .NET 3.5 is LINQ. "LINQ", stands for .NET Language Integrated Query.
LINQ supports a rich extensibility model that facilitates the creation of efficient domain-specific providers for data sources. .NET 3.5 ships with built-in libraries that enable LINQ support against Objects, XML, and Databases.
“Microsoft original motivation behind LINQ was to address the impedance mismatch between programming languages and database.”
What is LINQ to XML?
LINQ to XML is a built-in LINQ data provider that is implemented within the "System.Xml.Linq" namespace in .NET 3.5. Formerly LINQ is XLinq. It means Queries performed against the XML source. LINQ to XML provides a clean programming model that enables you to read, construct and write XML data. You can use LINQ to XML to perform LINQ queries over XML that you retrieve from the file-system, from a remote HTTP URL or web-service, or from any in-memory XML content.
Using LINQ to XML to read XML file:
using System;
using System.Xml.Linq;
using System.Text;

namespace ReadXMLData
{
  class Program  
	{
		static void Main(string[] args)
		{
			DisplayXmlData();

		}

public static void DisplayXmlData()
{
string languageDetails =

  @"                           ";

////Create XDocument object using string.
XDocument xmlDoc = XDocument.Parse(languageDetails);
/*
////Load XDocument object using xml document/file:
string xmlPath=""; //// Complete path of xml
XDocument xmlDoc = XDocument.Load(xmlPath);
*/
string languageTitle=GetLanguageTitle(xmlDoc);

////Find language xml

List languagesXml = GetLanguageXml(xmlDoc);

if (languagesXml != null)
     {
      ////Loop of language xml :: Begins
      foreach (XElement languageXml in languagesXml)
      {

       Console.WriteLine(languageXml.Attribute("URL").Value);
       Console.WriteLine(languageXml.Attribute("Name").Value);
      }
      ////Loop of language xml :: Ends
     }
}


 public static string GetLanguageTitle(XDocument xmlDoc)
  {
   string languageTitle = string.Empty;

   try
   {

    List objXElements = new List();
    objXElements = (from localeXml in xmlDoc.Descendants("LanguageTitle")
        select localeXml).ToList();

    if (objXElements != null)
    {
     languageTitle = objXElements[0].Attribute("Name").Value;
    }
   }
   catch (Exception ex)
   {

   }

   return languageTitle;
  }


  public static List GetLanguageXml(XDocument xmlDoc)
  {
   List objXElements = new List();

   try
   {

    objXElements = (from localeXml in xmlDoc.Descendants("Language")
        select localeXml).ToList();
   }
   catch (Exception ex)
   {

   }

   return objXElements;
  }

        }
}

Share:

0 comments:

About Me

My photo
Based in Bangalore, INDIA, Amit is a MCC. He has an MCA from the U.P.T.U., INDIA and holds Microsoft certifications including SharePoint 2007/2010/2013. He is an contributor to MSDN and http://amitkumarmca04.blogspot.com/. Amit have experience in SharePoint,ASP.Net with C#, Classic ASP, VB,Macros,HTML,JavaScript,JSON,BootStrap and AngularJS.

Followers

Labels