Hello,
Everybody i have a query about XML file!!!! well how to store/add the images to the xml file? and how to display it ?Plz help me to solve this.
Thanku.
Hello,
Everybody i have a query about XML file!!!! well how to store/add the images to the xml file? and how to display it ?Plz help me to solve this.
Thanku.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Blocked AccountRank #403 · 6.2K reputationPosted Aug 6, 2008, 9:37 PM EDT
Hi Swati
For storing the image in XML u have to follow the same approach as we use for storing the image in the database.
For storing the image in the xml use the following code on the click event of the button.
private void ButtonUpload_Click(object sender, System.EventArgs e)
{
Int64 len=file1.PostedFile.ContentLength;
Byte[] byt=new byte[len];
Stream st=file1.PostedFile.InputStream;
int n = st.Read(byt,0,(int)len);
DataSet ds=new DataSet();
ds.ReadXml(Server.MapPath("XMLFile1.xml"));
DataRow dr=ds.Tables[0].NewRow();
dr["id"]=TextBox1.Text;
dr["data"]=Implode(byt);
ds.Tables[0].Rows.Add(dr);
ds.WriteXml(Server.MapPath("XMLFile1.xml"));
bind();
}
public string Implode(Byte[] array)
{
//StringBuilder sb = new StringBuilder();
String data=String.Empty;
for (int i = 0; i < array.Length; ++i)
data+=array[i].ToString()+"#";
return data;
}
For Display the image use the following code.
string id = Request.QueryString["id"].ToString();
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("XMLFile1.xml"));
foreach (DataRow dr in ds.Tables[0].Rows)
{
if (dr["id"].ToString() == id)
{
try
{
string[] str = dr["data"].ToString().Split('#');
Byte[] byt = new byte[str.Length];
for (Int64 i = 0; i < str.Length - 1; i++)
byt[i] = Convert.ToByte(str[i].ToString());
Context.Response.ContentType = "image/";
Context.Response.BinaryWrite((byte[])byt);
break;
}
catch (Exception ex) { Console.Write(ex); }
}
}
Happy Coding!!