XDocument is Introduce in System.Xml.Linq namespace.
XDocument is more friendlier and easy to use than XMLDocument.
Earlier we are using XMLDocument for read and processing XML File.
Syntax of retrieving element from XML document is different than XMLDocument.
Every .Net Beginners needs to know this.
Here is example for this.
In this example We load Books.xml file and read data from this file using LINQ to XML and display in grid as well as display count of books.
XML File : (Books.xml)
ASPX Code :
C# Example :
VB.net Example :
Output :
XDocument is more friendlier and easy to use than XMLDocument.
Earlier we are using XMLDocument for read and processing XML File.
Syntax of retrieving element from XML document is different than XMLDocument.
Every .Net Beginners needs to know this.
Here is example for this.
In this example We load Books.xml file and read data from this file using LINQ to XML and display in grid as well as display count of books.
XML File : (Books.xml)
<?xml version="1.0" encoding="utf-8" ?> <Books> <Book> <Title>ASP.NET</Title> <ISBN>asp1</ISBN> <ReleaseDate>11/11/2010</ReleaseDate> <Pages>200</Pages> <PublisherId>1</PublisherId> </Book> <Book> <Title>C#.NET</Title> <ISBN>c#2</ISBN> <ReleaseDate>10/11/2010</ReleaseDate> <Pages>500</Pages> <PublisherId>1</PublisherId> </Book> <Book> <Title>VB.NET</Title> <ISBN>vb3</ISBN> <ReleaseDate>5/5/2009</ReleaseDate> <Pages>400</Pages> <PublisherId>1</PublisherId> </Book> <Book> <Title>SQL Server</Title> <ISBN>sql4</ISBN> <ReleaseDate>6/9/2010</ReleaseDate> <Pages>300</Pages> <PublisherId>2</PublisherId> </Book> <Book> <Title>JAVA</Title> <ISBN>java5</ISBN> <ReleaseDate>8/5/2011</ReleaseDate> <Pages>400</Pages> <PublisherId>3</PublisherId> </Book> </Books>
ASPX Code :
<form id="form1" runat="server"> <div> <asp:GridView ID="gvBooks" runat="server"> </asp:GridView> </div> </form>
C# Example :
XDocument objBooksXML = XDocument.Load(Server.MapPath("books.xml")); var objBooks = from book in objBooksXML.Descendants("Book") select new { Title = book.Element("Title").Value, Pages = book.Element("Pages").Value }; Response.Write(String.Format("Total {0} books.", objBooks.Count())); gvBooks.DataSource = objBooks; gvBooks.DataBind();
VB.net Example :
Dim objBooksXML As XDocument = XDocument.Load(Server.MapPath("books.xml")) Dim objBooks = From book In objBooksXML...<Book> Select New With {.Title = book.<Title>.Value, .Pages = book.<Pages>.Value} Response.Write(String.Format("Total {0} books.", objBooks.Count())) gvBooks.DataSource = objBooks gvBooks.DataBind()
Output :
Note : Give
Us your valuable feedback in comments. Give your suggestions in this
article so we can update our articles accordingly that.
useful. i guess xdocument is faster than xmldocument because of linq used there. m i right?
ReplyDeleteAlso, in any LINQ query what does "new" keyword stands for? (above in C# example). why do we use that?
Hi,
DeletexDocument is latest class and it's provide linq support so that easy to use.
"New" keyword is use for custom column projection. We can give custom column name.