Posted by : Sudhir Chekuri Wednesday, 28 October 2015

LINQ to XML is used to query data from xml document using simple queries so that we can save data from xml in other formats.

Example program for LINQ to XML:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace LINQApp
{
    class Program
    {
        static void Main(string[] args)
        {
            //xml data in saved in string
            string myXML = @"<Students>
                       <StudentName>Amir</StudentName>
                       <StudentName>Salman</StudentName>
                       <StudentName>Sharukh</StudentName>
                       <StudentName>Hrithik</StudentName>
                       </Students>";

            XDocument xdoc = new XDocument();
            //Converting data in string to XDocument format.
            xdoc = XDocument.Parse(myXML);
            //Quering all elements under Students tag
            var result = xdoc.Element("Students").Descendants();
            //Printing Values inside all retreived nodes(Nodes inside Students node)
            foreach (XElement item in result)
            {
                Console.WriteLine("Student Name - " + item.Value);
            }

            Console.ReadKey();
        }
    }
}

Example program to Add new xml tags in exiting xml document:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace LINQApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string myXML = @"<Courses>
                       <Course>PHP</Course>
                      <Course>JAVA</Course>
                      <Course>DOT NET</Course>
                       </Courses>";

            XDocument xdoc = new XDocument();
            xdoc = XDocument.Parse(myXML);

            //Add new Element or appending new element in xml document at the ending
            xdoc.Element("Courses").Add(new XElement("Course", "Phython"));

            //Add new Element at First
            xdoc.Element("Courses").AddFirst(new XElement("Course", "SQL Server"));

            var result = xdoc.Element("Courses").Descendants();

            foreach (XElement item in result)
            {
                Console.WriteLine("Course Name - " + item.Value);
            }
   
            Console.ReadKey();
        }
    }
}

Output:
SQL Server
PHP
JAVA
DOTNET
Phython

Example of LINQ to XML to remove or delete xml tag

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace LINQApp
{
    class Program
    {
        static void Main(string[] args)
        {//Creating xml document text in string
            string XML = @"<Courses>
<Course>DotNET</Course>
<Course>JAVA</Course>
<Course>PHP</Course>
<Course>SQL Server</Course>
</Courses>";

            XDocument xdoc = new XDocument();
            //Converting string type to XML Document format
            xdoc = XDocument.Parse(XML);
            //Querying to delete where course value matched with JAVA
            xdoc.Descendants().Where(s => s.Value == "JAVA").Remove();
            //Retreiving all courses from xml document
            var items = xdoc.Element("Courses").Descendants();
            //Printing all course names
            foreach (string s in items)
            {
                Console.WriteLine("Course Name: " + s);
            }
            Console.ReadKey();
        }
    }
}

Output
DotNET
PHP
SQL Server

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Followers

Total Pageviews

Powered by Blogger.

- Copyright © 2013 DevStudent - Metrominimalist - Powered by Blogger - Designed by Johanes Djogan -