How to insert/display image from XML file in asp.net?


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;
}
}
}

Advertisements
Posted in XML. 8 Comments »

8 Responses to “How to insert/display image from XML file in asp.net?”

  1. mistry jayendra Says:

    thnks for sharing

  2. Chandra Dev Says:

    You are always welcome.

  3. chairs for kids Says:

    I every time emailed this blog post page to all my associates, since if like to read it next my friends will too.

  4. my back hurts Says:

    I am really enjoying the theme/design of your web site. Do you ever run into any
    browser compatibility problems? A couple of my blog visitors have complained about my
    website not operating correctly in Explorer but looks great in
    Safari. Do you have any ideas to help fix this issue?

  5. waveless waterbed mattress Says:

    Hey would you mind sharing which blog platform you’re working with? I’m planning to start my own blog soon but I’m having a tough time choosing between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design and style seems different then most blogs and I’m looking for something unique.
    P.S Apologies for getting off-topic but I had to ask!

    • Chandra Dev Says:

      I m using wordpress.

  6. Sudhir Says:

    my code is

    XmlDocument xmldoc = new XmlDocument();
    xmldoc.Load(Server.MapPath(@”App_Code\xmlimage.xml”));
    XmlElement parentelement = xmldoc.CreateElement(“image”);
    XmlElement name = xmldoc.CreateElement(“url”);
    name.InnerText = FileUpload1.FileName.ToString() ;

    parentelement.AppendChild(name);

    xmldoc.DocumentElement.AppendChild(parentelement);
    xmldoc.Save(Server.MapPath(@”App_Code\xmlimage.xml”));
    it show the error An exception of type ‘System.IO.FileNotFoundException’ occurred in System.Xml.dll but was not handled in user code

  7. abadday.co.uk Says:

    you are in point of fact a just right webmaster. The web site
    loading speed is incredible. It kind of feels that you’re doing any distinctive trick.

    Also, The contents are masterpiece. you’ve performed a great
    activity in this topic!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: