HERE TODAY AM GONNA GIVE U VERY SIMPLE CODE...
I DONT BELIV IT HAS TO BE EXPLAINED BECOZ..ITS VERY MUCH UNDERSTANDABLE..
STILL ILL COMMENT OUT POINTS..WAT'S HAPPENIN IN CODE...
CREATE A FILE WITH NAME AS follows:-
Xmlreadrite.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="xmlinput.aspx.cs" Inherits="xmlinput" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Employee Information<br />
Name:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
Emp-Id:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
Qualification:<asp:DropDownList ID="DropDownList1" runat="server"
AutoPostBack="True">
<asp:ListItem>--SELECT--</asp:ListItem>
<asp:ListItem>MTEC</asp:ListItem>
<asp:ListItem>BE</asp:ListItem>
<asp:ListItem>BTEC</asp:ListItem>
<asp:ListItem>MBBS</asp:ListItem>
<asp:ListItem>BDS</asp:ListItem>
</asp:DropDownList>
<br />
<asp:Button ID="Button1" runat="server" Text="SUBMIT" onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="SHOW" onclick="Button2_Click" />
<br />
<br />
<asp:DataList ID="DataList1" runat="server" CellPadding="4" ForeColor="#333333">
<AlternatingItemStyle BackColor="White" ForeColor="#284775" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<ItemStyle BackColor="#F7F6F3" ForeColor="#333333" />
<ItemTemplate>
<hr />
Name:<%#DataBinder.Eval(Container.DataItem,"name")%><br />Emp-ID:<%#DataBinder.Eval(Container.DataItem,"Emp-Id")%>></a><br />
Qualification:<%#DataBinder.Eval(Container.DataItem,"Qualification")%>><br />
</ItemTemplate>
<SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
</asp:DataList>
</div>
</form>
</body>
</html>
Employee.xml
<?xml version="1.0" encoding="utf-8"?>
<Employeeinformation>
</Employeeinformation>
Xmlreadrite.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.Data;
using System.Xml;
public partial class xmlinput : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
here we retrive data from xml file to datalist by using dataset.
private void BindDatalist()
{
XmlTextReader xmlreader=new XmlTextReader(Server.MapPath("Employee.xml"));
DataSet da = new DataSet();
da.ReadXml(xmlreader);
xmlreader.Close();
if (da.Tables.Count != 0)
{
DataList1.DataSource = da;
DataList1.DataBind();
}
else
{
DataList1.DataSource = null;
DataList1.DataBind();
}
}
here we insert the data to xml file by creating xml object and assignin it to our xml file.
protected void Button1_Click(object sender, EventArgs e)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath("Employee.xml"));
XmlElement parentelement = xmldoc.CreateElement("Details");
XmlElement name = xmldoc.CreateElement("name");
name.InnerText = TextBox1.Text;
XmlElement EmpId = xmldoc.CreateElement("Emp-Id");
EmpId.InnerText = TextBox2.Text;
XmlElement Qualification = xmldoc.CreateElement("Qualification");
Qualification.InnerText = DropDownList1.SelectedItem.Text;
parentelement.AppendChild(name);
parentelement.AppendChild(EmpId);
parentelement.AppendChild(Qualification);
xmldoc.DocumentElement.AppendChild(parentelement);
xmldoc.Save(Server.MapPath("Employee.xml"));
}
here in this button click we call display function ,whic is binddatalist()
protected void Button2_Click(object sender, EventArgs e)
{
BindDatalist();
}
}
Is there any way to convert this to a .txt file??
ReplyDeleteyou give the extension of a file as ".txt"
DeleteHi , Please let me what do you want to convert ??
ReplyDeleteif you are thinking reading contents from text file , you can use stream reader.
could u plz tell me how to do that?? i m completely new to this topic..how can i use streamreader?
Deletecan you please tell what is your requirement ??
Deletesuch that i can help you out
Here is simple way to read contents from text file :-
Delete//create a stream reader object and pass the file path
string path = "pass the path where file is stored";
Textreader txtReader = new StreamReader(path );
//read lines
tr.ReadLine();
//close the text reader
tr.Close();
I am presently working on asp.net. My supervisor asked me to convert an xml file to a .txt file..and I dont knw hw to exactly do that..
ReplyDeletecan it be done using visual studio 2010..I mean can i create an xml file and then convert and save it as .txt file??
ReplyDeleteYes of course, You need read the XML in XML reader and save it as text file in stream writer..
ReplyDeletebut tell you want save whole data as is in XML format to text??
No not the whole data..only keywords..for example:
ReplyDeletename-xxx
id-xxx
This comment has been removed by the author.
DeleteHope this helps you,
Deletethis is what i did , when i has a requirement of parsing and reading xml,
but be very care full while reading ,,use proper null checks before your read
XmlNode node = new XmlNode();//load your xml here
public getContentItem(XmlNode node){
foreach (XmlNode chldNode in node)
{
try
{
if (chldNode.HasChildNodes){
getContentItem(chldNode);
}
if (chldNode.Attributes != null && chldNode.Attributes["UseDefault"] != null)
{
//access value using
chldNode.Attributes["DefaultGUID"].Value;
//keep appending data to string builder
}
}
catch (Exception ex)
{
//log error
}
}
}