XML is well known format to hold data. It is widely accepted all over the world. Formatting XML data depend upon the node ,attribute, elements ect . You can easily access , modify node , attribute , elements through programming languages. LINQ to XML is it functionality of.Net framework to query and modify the XML in memory. LINQ to XML increase the productivity, developer need to write less code and it is compact. To implement LINQ to XML, you need to load XML in memory first, then only you can modify or query in the XML document. The advantage of these approaches is, the process is integrated with languages and very lightweight, during the compilation of program , checking is done over the query and modification. After the query or notification is over , now you can save the XML document to physical file and you can do the necessary work with memory XML file. System.Xml.Linq is the namespace is used for XML manipulation.
Below Example Show
1)A Simple select query ,return list of student.
2)A Simple select query with where condition,return list of student whose marks greater than 15
3)A Simple select query with where condition,return list of student whose Roll is 14.
Example
sb.Append("");
Below Example Show
1)A Simple select query ,return list of student.
2)A Simple select query with where condition,return list of student whose marks greater than 15
3)A Simple select query with where condition,return list of student whose Roll is 14.
Example
using
System;
using
System.Collections.Generic;
using
System.Data.SqlClient;
using
System.Data.Sql;
using
System.Data;
using
System.Linq;
using
System.Collections;
using
System.Xml;
using
System.Text;
using
System.Xml.Linq;
using
System.IO;
namespace
ConsoleApplication1
{
class
Program
{
static
void
Main(string[]
args)
{
StringBuilder
sb
= new
StringBuilder();
sb.Append("");
sb.Append("");
sb.Append("John1");
sb.Append("12");
sb.Append("12.50");
sb.Append("");
sb.Append("");
sb.Append("John2");
sb.Append("13");
sb.Append("13.50");
sb.Append("");
sb.Append("");
sb.Append("John3");
sb.Append("14");
sb.Append("14.50");
sb.Append("");
sb.Append("");
sb.Append("John4");
sb.Append("14");
sb.Append("14.50");
sb.Append("");
sb.Append("");
sb.Append("John5");
sb.Append("15");
sb.Append("15.50");
sb.Append("");
sb.Append("");
sb.Append("John6");
sb.Append("16");
sb.Append("16.50");
sb.Append("");
sb.Append("");
sb.Append("John7");
sb.Append("17");
sb.Append("17.50");
sb.Append("");
sb.Append("");
TextReader
tr
= new
StringReader(sb.ToString());
XDocument
xdoc
= XDocument.Load(tr);
/******************LINQ
to XML --Simple Select Query*******************/
Console.WriteLine("*********************List
of Student*******************");
var
lv1s
= from
lv1
in
xdoc.Descendants("student")
select
new
{
Name
= lv1.Element("Name").Value,
Roll
= lv1.Element("Roll").Value,
Marks
= lv1.Element("Marks").Value
};
foreach
(var
lv1
in
lv1s)
{
Console.WriteLine("Name
= {0} , RollNumber = {1} ,Marks = {1} ",
lv1.Name,
lv1.Roll, lv1.Marks);
}
Console.ReadKey();
Console.WriteLine("*********************List
of Student, Marks greated than 15***********");
var
lv1a
= from
lva
in
xdoc.Descendants("student")
where
Convert.ToDecimal(lva.Element("Marks").Value)
>= 15
select
new
{
Name
= lva.Element("Name").Value,
Roll
= lva.Element("Roll").Value,
Marks
= lva.Element("Marks").Value
};
foreach
(var
lv1
in
lv1a)
{
Console.WriteLine("Name
= {0} , RollNumber = {1} ,Marks = {1} ",
lv1.Name,
lv1.Roll, lv1.Marks);
}
Console.ReadKey();
Console.WriteLine("*********************List
of Student, Filter by Roll=14***********");
IEnumerable<XElement>
stu = from
el
in
xdoc.Elements("student")
where
(string)el.Element("Roll")
== "14"
select
el;
foreach
(var
stuElm
in
stu)
{
Console.WriteLine("Name
= {0}",
stuElm.Name);
}
Console.ReadKey();
}
}
}
Output :
*********************List of Student*******************
Name = John1 , RollNumber = 12 ,Marks = 12
Name = John2 , RollNumber = 13 ,Marks = 13
Name = John3 , RollNumber = 14 ,Marks = 14
Name = John4 , RollNumber = 14 ,Marks = 14
Name = John5 , RollNumber = 15 ,Marks = 15
Name = John6 , RollNumber = 16 ,Marks = 16
Name = John7 , RollNumber = 17 ,Marks = 17
*********************List of Student, Marks greated than
15***********
Name = John5 , RollNumber = 15 ,Marks = 15
Name = John6 , RollNumber = 16 ,Marks = 16
Name = John7 , RollNumber = 17 ,Marks = 17
*********************List of Student, Filter by Roll=14***********
No comments:
Post a Comment