Description: XML
File is nothing but extensible markup language. This is store data in markup
language in file so we can read each data in separated node in Xml file. Here
we will create FileUpload Controls and button and label controls to Upload file
in server and read and display Xml file
in Browser if any error comes then we can display in label control. For more we
can set some validation for FileUpload controls only xml file are allowed for
upload in server otherwise not. So let’s start how to achieve this functionality.
Step 1: Drag FileUpload, Button and label
Control in Webpage.
<div> <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID="btnViewXML" runat="server" Text="View XML" OnClick="ViewXML" /> <br /> <asp:Label ID="lblmessage" runat="server" Text=""></asp:Label> </div>
Step 2: Create Folder in your Web Application and Name it “Uploads”.
Step 3: Now Generate button Click event and
Check for FileUpload and for extension and if it’s right then read Xml File and
Display in Browser.
protected void ViewXML(object sender, EventArgs e) { if (FileUpload1.HasFile) { string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName); string path1 = Server.MapPath("~/Uploads/") + FileName; FileUpload1.SaveAs(path1); string Extension = Path.GetExtension(FileUpload1.PostedFile.FileName); if (Extension == ".xml") { Response.Clear(); Response.Buffer = true; Response.Charset = ""; Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.ContentType = "application/xml"; Response.WriteFile(Server.MapPath("~/Uploads/" + FileName)); Response.Flush(); Response.End(); } else { lblmessage.Text = "Please Upload Xml File !"; lblmessage.ForeColor = System.Drawing.Color.Red; } } else { lblmessage.ForeColor = System.Drawing.Color.Red; lblmessage.Text = "File Not Selected for Upload !"; } }
You can Set Label Control Property in Page_load
() for Null Value When Page_load First time.
protected void Page_Load(object sender, EventArgs e) { lblmessage.ForeColor = System.Drawing.Color.Black; lblmessage.Text = string.Empty; }
Step 4: Now run your Application in browser and Upload File. Here you can check with other file but it will not allow for Upload.
Step 5: Now Click on View XML File to
Display XML File in Browser.
Thanks for comments.....