ARTICLE

Run time XML File create and Image upload and save in VB.NET.

Posted by Sapna Malik Articles | Visual Basic 2010 August 03, 2010
Tags: .NET, ASP.NET, VB.NET
In this article, We will see how to create XML File in run time and Image Upload, Save in VB.NET.
Download Files:
 
Reader Level:

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

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor