
We can do this task like this
Step1: create the Xml file in asp.net project like this
<?xml version=”1.0″ encoding=”utf-8″?>
<ImgTest>
<EmpDetails>
<EmpName> </EmpName>
<EmpSal> </EmpSal>
<ImgUrl> </ImgUrl>
</EmpDetails>
</ImgTest>
Step2: Create one Image Folder for storing the Image i.e UploadImages
Step3: Create the corresponding design page in asp.net and take one grid view to display the Emp detail with image. Configure gridview like this
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="#">
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="EmpName" DataField="EmpName" />
<asp:BoundField HeaderText="EmpSal" DataField="EmpSal" />
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<asp:Image ID="Img1" runat="server" ImageUrl='<%# String.Format("~\\UploadImages\\{0}",Eval("ImgUrl"))%>’ Height="100px" Width="100px" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Note: here I m storing imgae in “UploadImages” folder and ImageName in database
Step4:On Button click, write the code in code behind like this
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.IO;
public partial class XmlImg : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
//This code is for insertion in XML file
protected void BtnSubmit_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
if (CheckFileType(FileUpload1.FileName))
{
String filePath = “~/UploadImages/” + FileUpload1.FileName;
FileUpload1.SaveAs(MapPath(filePath));
}
}
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath(@”App_Data\ImgTest.xml”));
XmlElement newEmpDetails= xmldoc.CreateElement(“EmpDetails”);
XmlElement XmlEmpName= xmldoc.CreateElement(“EmpName”);
XmlElement XmlEmpSal = xmldoc.CreateElement(“EmpSal”);
XmlElement XmlImgUrl = xmldoc.CreateElement(“ImgUrl”);
XmlEmpName.InnerText = this.TextBox1.Text.Trim();
XmlEmpSal.InnerText = this.TextBox2.Text.Trim();
string vbfile = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName.ToString());
XmlImgUrl.InnerText = FileUpload1.FileName.ToString();
newEmpDetails.AppendChild(XmlEmpName);
newEmpDetails.AppendChild(XmlEmpSal);
newEmpDetails.AppendChild(XmlImgUrl);
xmldoc.DocumentElement.AppendChild(newEmpDetails);
xmldoc.Save(Server.MapPath(@”App_Data\ImgTest.xml”));
TextBox1.Text = “”;
TextBox2.Text = “”;
fillGrid();
}
//This code is filling the Gridview
protected void fillGrid()
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath(@”App_Data\ImgTest.xml”));
if (ds.Tables.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
//This code is for checking the file extension
bool CheckFileType(string fileName)
{
string ext = Path.GetExtension(fileName);
switch (ext.ToLower())
{
case “.gif”:
return true;
case “.png”:
return true;
case “.jpg”:
return true;
case “.jpeg”:
return true;
default:
return false;
}
}
}
12.971599
77.594563