My new blog present here.

Featured Post

Insights into Sitecore Search: A Definitive Introduction

A key component of digital experience management is effective information retrieval. A simplified approach is required for websites, applications, and platforms containing a lot of material so that consumers can easily get the data they require. This need is met by Sitecore, a well-known name in the field of digital experience platforms (DXPs), which provides powerful and comprehensive search functionality. We will travel into the realm of Sitecore Search in this article, learning about its capabilities, architecture , and the enormous value it offers both developers and end users. Introduction to Sitecore Search    A headless content discovery platform powered by AI , Sitecore Search enables you to build predictive and custom search experiences across various content sources. To extract and ...

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;
  }

        }
}

Comments

Popular posts from this blog

Sitecore GraphQL Queries

Sitecore Experience Manager Cloud (XM Cloud) Building blocks

Configuring Sitecore Next.js Headless SXA Multisite App in a Sitecore Container