We can read the XML based on some condition from any XML data source by using XMLNodeList and XMLNode. Here we discuss about how to read XML file by using XMLDocument and how to read specific node information by using XMLNodeList or XMLNode.
There are two ways to read specific XML node based on condition, here we discuss about two ways. Before going to discuss about two methods, first we will add three textboxes, one button and one XML file. One textbox is for input and another two textboxes are to display output as shown below.
default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="CSharpReadXML._default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Read XML Node information by using XML Node</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<label>Enter Employee Id: </label> <asp:TextBox ID="txtEmpId" runat="server"></asp:TextBox><br />
<asp:Button ID="btn" runat="server" OnClick="btn_Click" Text="Search" /><br /><br />
<label>Employee Name: </label> <asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />
<label>Employee Salary: </label> <asp:TextBox ID="txtSalary" runat="server"></asp:TextBox><br />
</div>
</form>
</body>
</html>
Employees.xml
<?xmlversion="1.0"encoding="utf-8" ?>
<Employees>
<Employee>
<Id>1</Id>
<Name>A</Name>
<Salary>10000</Salary>
</Employee>
<Employee>
<Id>2</Id>
<Name>B</Name>
<Salary>20000</Salary>
</Employee>
<Employee>
<Id>3</Id>
<Name>C</Name>
<Salary>15000</Salary>
</Employee>
<Employee>
<Id>4</Id>
<Name>D</Name>
<Salary>25000</Salary>
</Employee>
</Employees>
First Method:
Here we read the XML file content by using XMLDocument. Then we will get the all XML nodes of specific type by using SelectNodes method of XMLDocument. We will loop the XMLNodeList by using for-each statement and we will check the each node value as shown below.
default.aspx.cs
using System;
using System.Xml;
namespace CSharpReadXML
{
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_Click(object sender, EventArgs e)
{
XmlDocument xmlEmployee = new XmlDocument();
xmlEmployee.Load(Server.MapPath("XML/Employees.xml"));
XmlNodeList xnList = xmlEmployee.SelectNodes("/Employees/Employee");
foreach (XmlNode xNode in xnList)
{
if (xNode["Id"].InnerText == txtEmpId.Text)
{
txtName.Text = xNode["Name"].InnerText;
txtSalary.Text = xNode["Salary"].InnerText;
}
}
}
}
}
As shown above we are comparing the each Employee node Id value with the input data and if it is satisfies the condition, fetch the particular node information.
This is easy way to get the specific node information, but time consuming and not good for performance wise.
Second Method:
In this method also we read the XML file information by using XMLDocument and instead of looping all XML nodes by using XMLNodeList, we directly seek for the specific node information based on its value as shown below.
default.aspx.cs
using System;
using System.Xml;
namespace CSharpReadXML
{
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_Click(object sender, EventArgs e)
{
XmlDocument xmlEmployee = new XmlDocument();
xmlEmployee.Load(Server.MapPath("XML/Employees.xml"));
XmlNode xnList = xmlEmployee.SelectSingleNode("/Employees/Employee[Id = " + txtEmpId.Text + "]");
txtName.Text = xnList["Name"].InnerText;
txtSalary.Text = xnList["Salary"].InnerText;
}
}
}
As shown above we are fetching the required Employee node by passing the value for Id. This way of fetching the XML node information is very fast while comparing to the first method.