Introduction:
Here
I will explain how to count number of records in xml
file in C# using asp.net or how to count
number of nodes in xml file in asp.net using C# and VB.NET
or count number of elements in xml file in C#.
Description:
In
previous articles I explained insert xml data to sql table using stored procedure, Bind xml data to dropdown/gridview in asp.net, create online poll system with percentage graphs in asp.net
and many articles relating to xml,
Gridview, SQL, jQuery,asp.net, C#,VB.NET. Now I will explain how
to count number of records in xml
file in C# using asp.net.
XmlDocument readDoc = new
  XmlDocument(); 
readDoc.Load(MapPath("Sample.xml")); 
int count = readDoc.SelectNodes("CommentsInformation/Comments").Count; 
lblcount.InnerHtml = "Number of Records: "+ count; 
 | 
 
If
you want to see it in complete example we need to write the code like as shown
below
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title></title> 
</head> 
<body> 
<form id="form1"
  runat="server"> 
<div> 
<table> 
<tr> 
<td style="width: 100px"> 
Name:</td> 
<td style="width: 100px"> 
<asp:TextBox ID="txtName"
  runat="server"></asp:TextBox></td> 
</tr> 
<tr> 
<td style="width: 100px"> 
Email:</td> 
<td style="width: 100px"> 
<asp:TextBox ID="txtEmail"
  runat="server"></asp:TextBox></td> 
</tr> 
<tr> 
<td></td> 
<td> 
<asp:Button ID="btnSubmit"
  runat="server"
  OnClick="btnSubmit_Click"
  Text="Submit"
  /></td> 
</tr> 
</table> 
<br /> 
<label id="lblcount"
  runat="server"
  /> 
</div> 
</form> 
</body> 
</html> 
 | 
 
After
that add XML file to your application and give name as "Sample.xml"
then add root element in xml file otherwise it will through error. Here I
added CommentInformation as root
element in XML file.
<?xml version="1.0" encoding="utf-8"?> 
<CommentsInformation> 
 </CommentsInformation> 
 | 
 
After
that add this namespace in codebehind
C#
Code
using System; 
using System.Xml; 
 | 
 
After that add below code in code behind
protected void
  Page_Load(object sender, EventArgs e) 
{ 
} 
protected void
  btnSubmit_Click(object sender, EventArgs e) 
{ 
XmlDocument xmldoc = new
  XmlDocument(); 
xmldoc.Load(Server.MapPath("Sample.xml")); 
XmlElement parentelement = xmldoc.CreateElement("Comments"); 
XmlElement name = xmldoc.CreateElement("Name"); 
name.InnerText = txtName.Text; 
XmlElement email = xmldoc.CreateElement("Email"); 
email.InnerText = txtEmail.Text; 
parentelement.AppendChild(name); 
parentelement.AppendChild(email); 
xmldoc.DocumentElement.AppendChild(parentelement); 
xmldoc.Save(Server.MapPath("Sample.xml")); 
XmlDocument readDoc = new
  XmlDocument(); 
readDoc.Load(MapPath("Sample.xml")); 
int count = readDoc.SelectNodes("CommentsInformation/Comments").Count; 
lblcount.InnerHtml = "Number
  of Records: "+ count; 
} 
 | 
 
VB.NET
Code
Imports System.Xml 
Partial Class vbcode 
Inherits System.Web.UI.Page 
Protected Sub
  Page_Load(ByVal sender As Object, ByVal e As
  EventArgs) 
End Sub 
Protected Sub
  btnSubmit_Click(ByVal sender As Object, ByVal e As
  EventArgs) 
Dim xmldoc As New XmlDocument() 
xmldoc.Load(Server.MapPath("Sample.xml")) 
Dim parentelement As
  XmlElement = xmldoc.CreateElement("Comments") 
Dim name As XmlElement =
  xmldoc.CreateElement("Name") 
name.InnerText = txtName.Text 
Dim email As XmlElement =
  xmldoc.CreateElement("Email") 
email.InnerText = txtEmail.Text 
parentelement.AppendChild(name) 
parentelement.AppendChild(email) 
xmldoc.DocumentElement.AppendChild(parentelement) 
xmldoc.Save(Server.MapPath("Sample.xml")) 
Dim readDoc As New XmlDocument() 
readDoc.Load(MapPath("Sample.xml")) 
Dim count As Integer = readDoc.SelectNodes("CommentsInformation/Comments").Count 
lblcount.InnerHtml = "Number
  of Records: " & count 
End Sub 
End Class 
 | 
 
Demo
Download
Sample Code Attached
| 
 If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.  | 
|||
 
                                        Subscribe by RSS
                                      
 
                                        Subscribe by Email
                                      
 | 
|||


                                        Subscribe by RSS
                                      
                                        Subscribe by Email
                                      
0 comments :
Note: Only a member of this blog may post a comment.