LINQ With XML File
First Start Visual Studio, Then Select File -> New -> Project
Then a dialog box appear, Choose the Console Application
After that give the name of application as i did "test" my application name.
Then goto the "Project" Option and select "Add New Item" a dialog box appear
the select the "XMLFile.xml"
Then Write the Following Code to the XmlFile1.xml
<?xml version="1.0" encoding="utf-8" ?>
<Employees>
<Employee Id="1">
<Name>Amit</Name>
<Address>#111</Address>
</Employee>
<Employee Id="2">
<Name>Balkar</Name>
<Address>#222</Address>
</Employee>
<Employee Id="3">
<Name>Chander</Name>
<Address>#333</Address>
</Employee>
</Employees>
Then Write the Following Code to the Program.cs
using System;
using System.Linq;
using System.Xml.Linq;
using System.IO;
class LINQToXML {
static void Main() {
XDocument xdoc = XDocument.Load(@"C:\Users\seven\Desktop\test\test\XMLFile1.xml");
var result = from c in xdoc.Descendants("Employee") select c;
foreach (var r in result)
{
Console.WriteLine("Id " + r.Attribute("Id").Value +
" Name " + r.Element("Name").Value +
" Address " + r.Element("Address").Value + "\n");
}
Console.ReadLine();
}
}
OUTPUT
LINQ With XML File
Reviewed by Baljeet Singh
on
15:23
Rating:
No comments: