Tuesday, September 06, 2011

Render(Parse) XML using XSLT

Requirement:
The requirement is to render xml data into a page.
Resolution:
We can perform this task by following ways:
1. Read xml file using C#.Net and iterate the xml node. At the time of iteration, render data from xml to page.
2. Use the XSL. In this approach, create .XSLT file, which store the formatting in the form of HTML/CSS. By using XSL, we can separate the data rending from the data itself.
Example to render (parse) xml using xslt:
Save given xml in to file called "myNavigation.xslt":



  
    
  



Now add following tag in your .ASPX page:


In the code behind file of .ASPX page (.ASPX.CS file), add following directive:
using System.Xml;
using System.Xml.Xsl;

After that,in the code behind file of .ASPX page (.ASPX.CS file), write following code in the Page_Load method:

string languageDetails =
 @"
    
        
            
                
                
                
            
            
                
                
            
            
            
            
                
                
                
                
                
              
            
            
              
            
        
      ";
XmlDocument myNavigationDoc = new XmlDocument();
myNavigationDoc.LoadXml(languageDetails);

XslTransform myNavigationTransform = new XslTransform();
myNavigationTransform.Load(Server.MapPath("myNavigation.xslt"));

this.myNavigationControl.Document = myNavigationDoc;
this.myNavigationControl.Transform = myNavigationTransform;

Share:

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:

Sitecore® Technology MVP 2019

Featured Post

Sitecore-Front-end performance tuning points

In the previous Sitecore Performance Tuning article, i tried to explain about that Sitecore website performance depends upon Front-end ...

Total Pageviews

State Counter

Popular

About Me

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

Followers

Follow me on Twitter