Create XML file at runtime and Image upload and save in VB.NET

In this article, We will see how to create XML File in run time and Image Upload, Save in VB.NET.
  • 768
Download Files:
 

In this article, We will see how to create XML File in run time and Image Upload, Save in VB.NET.

This code is aspx :-

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<
body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td colspan="2">
                    <asp:Label ID="lblMessage" runat="server" Font-Names="Verdana" Font-Size="Small"
                        ForeColor="Red"></asp:Label>
                </td>
            </tr>
            <tr id="tr1" runat="server">
                <td style="width: 163px">
                    <span style="font-size: 10pt; font-family: Verdana"><strong>Select file to upload:</strong></span>
                </td>
                <td style="width: 324px">
                    <asp:FileUpload ID="fileUpload" runat="server" />
                    &nbsp;
                    <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
                </td>
            </tr>
            <tr>
                <td style="width: 163px">
                </td>
                <td style="width: 324px">
                    <asp:ListBox ID="lstFiles" runat="server" Width="324px"></asp:ListBox>
                </td>
            </tr>
            <tr>
                <td style="width: 163px">
                </td>
                <td style="width: 324px">
                   <asp:Button ID="btnRemove" runat="server" Text="Remove" OnClick="btnRemove_Click"/>
                    &nbsp;<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
                </td>
            </tr>
        </table>
        <table>
            <tr>
                <td>
                   <asp:DataList ID="DataListImage" Visible="true" RepeatColumns="4" Width="100%" Height="50%" runat="server">
                        <ItemTemplate>
                            <table>
                                <tr>
                                    <td>
                                        <asp:Image ID="Image1" ImageUrl='<%#Eval("Name","Images/{0}")>'  Width="250px" Height="250px" runat="server" BorderColor="#006600" />
                                    </td>
                                </tr>
                            </table>
                        </ItemTemplate>
                    </asp:DataList>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</
html>

 This code is .vb :-

Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Configuration
Imports System.Web.Security
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.IO
Imports System.Data.SqlClient
Imports System.Collections
Imports System.Xml

-------------------------------------------------------------------------------------------------------

Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Shared arrFiles As New ArrayList()
    Protected isUploaded As Integer = 0
    Protected pathToUpload As String = HttpContext.Current.Server.MapPath("Images/")

-------------------------------------------------------------------------------------------------------
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    End Sub

-------------------------------------------------------------------------------------------------------

    Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpload.Click
        Dim FileName As String = Server.MapPath("ImageXMLFile.xml")
        If (lstFiles.Items.Count = 0) AndAlso (isUploaded = 0) Then
            lblMessage.Text = "Please specify file name"
        Else
            For Each Ipf As System.Web.UI.WebControls.FileUpload In arrFiles
                Try
                    Dim strFileName As String = System.IO.Path.GetFileName(Ipf.PostedFile.FileName)
                    Ipf.PostedFile.SaveAs(pathToUpload & "\" & strFileName)
                    isUploaded = isUploaded + 1
                    Dim xmlDoc As New XmlDocument()
                    xmlDoc.Load(FileName)
                    Dim xmlnode As XmlNodeList = xmlDoc.GetElementsByTagName("Image")                   
                    Dim
Root As XmlNode = xmlDoc.DocumentElement
                    Dim Page As XmlElement = xmlDoc.CreateElement("Image")
                    Dim ImageName As XmlElement = xmlDoc.CreateElement("ImageName")
                    Dim textImageName As XmlText = xmlDoc.CreateTextNode("Hello")
                    textImageName.Value = System.IO.Path.GetFileName(Ipf.PostedFile.FileName)
                    Dim URL As XmlElement = xmlDoc.CreateElement("URL")
                    Dim textURL As XmlText = xmlDoc.CreateTextNode("Hello2")
                    textURL.Value = pathToUpload & strFileName
                    Dim CreateDate As XmlElement = xmlDoc.CreateElement("CreateDate")
                    Dim textCreateDate As XmlText = xmlDoc.CreateTextNode("Hello3")
                    textCreateDate.Value = DateTime.Now.ToString().Trim()
                    Root.AppendChild(Page)
                    Page.AppendChild(URL)
                    URL.AppendChild(textURL)
                    Page.AppendChild(ImageName)
                    ImageName.AppendChild(textImageName)
                    Page.AppendChild(CreateDate)
                    CreateDate.AppendChild(textCreateDate)
                     xmlDoc.Save(FileName)
                Catch ex As Exception
                    lblMessage.Text = "An error has occured while uploading your files:<br>" &
ex.Message
                End Try
            Next
            If isUploaded = arrFiles.Count Then
                lblMessage.Text = "Files uploaded successfully"
            End If
            arrFiles.Clear()
            lstFiles.Items.Clear()
        End If
    End Sub

-------------------------------------------------------------------------------------------------------

    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs)
        Try
            If Page.IsPostBack Then
                arrFiles.Add(fileUpload)
                lstFiles.Items.Add(fileUpload.PostedFile.FileName)
            End If
        Catch ex As Exception
            lblMessage.Text = "An error has occured while adding file" & ex.Message
        End Try
    End Sub

    Protected Sub btnRemove_Click(ByVal sender As Object, ByVal e As EventArgs)
        If lstFiles.Items.Count <> 0 Then
            arrFiles.Remove(fileUpload)
            lstFiles.Items.Remove(lstFiles.SelectedItem.Text)
        End If
    End Sub

-------------------------------------------------------------------------------------------------------

    Protected Sub Page_Prerender(ByVal sender As Object, ByVal e As EventArgs)
        Dim fileInfo As FileInfo()
        Dim UpFolder As String = Server.MapPath("Images/")
        Dim dir As New DirectoryInfo(UpFolder)
        fileInfo = dir.GetFiles()
        Dim TotalImages As Integer = fileInfo.Count()
        For count As Integer = 0 To TotalImages - 1
            Dim image As New Image()
            image.ImageUrl = "Images/" & fileInfo(count).ToString()
            image.ID = count.ToString()
            DataListImage.Controls.Add(image)
        Next
        DataListImage.DataSource = dir.GetFiles()
        DataListImage.DataBind()
    End Sub
End Class

-------------------------------------------------------------------------------------------------------

Output:-

xmlfile.bmp

Categories

More Articles

© 2013 dotNetheaven. All rights reserved.