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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Blocked AccountPosted Aug 7, 2008, 1:37 AM
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!!