Demo.xml
<?xml version="1.0"?>
<Details>
<ID>1</ID>
<FirstName>Rupam</FirstName>
<LastName>Gupta</LastName>
<Salary>500000</Salary>
</Details>
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs"
Inherits="Practical10._default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</div> <p>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="XML Writer" />
</p>
<asp:ListBox ID="ListBox1" runat="server" Height="149px"
Width="220px"></asp:ListBox>
<p>
</p>
<p>
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="XML Reader" />
</p>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq; using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
namespace Practical10
{
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
XmlTextWriter writer = new
XmlTextWriter("C:\\Users\\RupamSolution\\source\\repos\\Practical10\\Practical10\\Demo.xml"
, null);
writer.WriteStartDocument();
writer.WriteStartElement("Details", "");
writer.WriteElementString("ID", "1");
writer.WriteElementString("FirstName", "Rupam");
writer.WriteElementString("LastName", "Gupta");
writer.WriteElementString("Salary", "500000");
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
Label1.Text = "Data Inserted Successfully";
}
protected void Button2_Click(object sender, EventArgs e)
{
String xmlNode =
"C:\\Users\\RupamSolution\\source\\repos\\Practical10\\Practical10\\Demo.xml";
XmlReader xReader = XmlReader.Create(xmlNode);
while(xReader.Read())
{
switch(xReader.NodeType)
{
case XmlNodeType.Element:
ListBox1.Items.Add("<" + xReader.Name + ">");
break;
case XmlNodeType.Text:
ListBox1.Items.Add(xReader.Value);
break;
case XmlNodeType.EndElement:
ListBox1.Items.Add("<" + xReader.Name + ">");
break;
}
}
}
}
}
0 Comments