1.

How to read xml document by using linq

Answer»

In Below example we will read xml document in c# using linq. First i have created a xml file with below content


< ?xml version="1.0" encoding="utf-8" ?>
< websites xmlns="URN:websitename">
< websitename>getproductprice.com< /websitename>
< websitename>interviewqsn.com< /websitename>
< /websites>


To read above xml file we will write below code and name of ABOV file is websites.xml

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace LinqExample
{
class websitename
{
STATIC void Main(string[] args)
{
var doc = XDocument.Load("websites.xml");
var QUERY = from x in doc.Descendants("{urn:websitename}websitename")
select x;
foreach (var item in query)
{
Console.WriteLine(item);
}
}
}
}



Discussion

No Comment Found