vijay

welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Wednesday, July 27, 2011

Insert/edit/delete in Xml using Asp.net


Hi..
Its very interesting when talk about Xml(extensible Markup language.).Xml can support for all language. its used to design and transport the data. important to know and easy to learn. Now we discuss about to insert, update and delete in xml.
Xml  act as database. So we can store any information in xml.Also we can retrieve  information by using any language. Here I store the image in xml,without using database I store the image url in XML.
For that create an folder an image in project. So we can store the image in that folder and save the image url in xml.
Create one xml file in the name imagefile.xml
Initially add an gridview ,button and one link button in client side ,Hence Coding will be look like below……
<div>
    <table cellpadding ="0" cellspacing ="0" width="500" align="center" >
    <tr>
    <td height="100">
     
    </td>
   
    </tr>
    <tr>
    <td>
    Name:
    </td>
    <td>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </td>
   
    </tr>
    <tr>
    <td>
    Image:
    </td>
    <td>
    <asp:FileUpload ID="FileUpload1" runat="server" />
    </td>
    </tr>
    <tr>
    <td height="30">
   
    </td>
   
    </tr>
    <tr>
    <td  align="justify"  >
   
        <asp:Button ID="Button1" runat="server" Text="ADD" />
    </td>
    <td>
        <asp:Label ID="Label1" runat="server"></asp:Label>
    </td>
    </tr>
    <tr>
    <td colspan ="2">
    <asp:Button ID="Button2" runat="server" Text="Show all" />
    </td>
       
    </tr>
    <tr>
    <td colspan ="2">
        <asp:GridView ID="imggrid" runat="server" AutoGenerateColumns ="false" Width="250px" DataKeyNames ="ID">
        <Columns>
        <asp:TemplateField HeaderText ="id">
         <ItemTemplate>
        <%#Eval("ID")%>
        </ItemTemplate>
              
        </asp:TemplateField>
        <asp:TemplateField HeaderText ="Name" >
        <ItemTemplate>
        <%#Eval("imagename")%>
        </ItemTemplate>
       
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Image">
        <ItemTemplate >
            <asp:Image ID="Image1" runat="server" ImageUrl ='<%#eval("URL") %>' height="100px" Width="100px" />
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField Headertext="Delete">
        <ItemTemplate >
                    <asp:LinkButton ID="lnkdelete" runat="server" CommandName ="Delete">Delete</asp:LinkButton>
        </ItemTemplate>
       
        </asp:TemplateField>
        </Columns>
        </asp:GridView>
       
   
    </td>
   
    </tr>
    <tr>
    <td>
  
       
    </td>
      
    </tr>
   
    </table>
    </div>


Xml structure should be like below…

<?xml version="1.0" encoding="utf-8"?>
<picture>
</picture>

In the client side write the codeing in load event

        Dim filename As String
        Dim imgext As String
        Dim imgpath As String
        Dim y As String
        Dim i As String

        xmlpath = Server.MapPath("~/imagefile.xml")
        If FileUpload1.HasFile Then
            imgext = FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf(".") + 1).ToLower


            filename = TextBox1.Text
            filename = FileUpload1.FileName

            If (imgext = "jpg" Or imgext = "gif" Or imgext = "bmp") Then
                imgpath = "~/image/" + filename
                FileUpload1.SaveAs(Server.MapPath("~/image/") + filename)
                'Label1.Text = Server.MapPath("~/image/") + filename
            Else
                Label1.Text = "update only jpg,gif and bmp file"
            End If
            Dim xmldoc As New XmlDocument
            xmldoc.Load(xmlpath)
            Dim max As String

            Dim nav As XPathNavigator = xmldoc.CreateNavigator()
            Dim sort As XPathExpression = nav.Compile("picture/image")
            sort.AddSort("ID", XmlSortOrder.Descending, XmlCaseOrder.None, "", XmlDataType.Number)
            max = nav.SelectSingleNode(sort).SelectSingleNode("ID").Value
            max += 1

            Dim root As XmlNode = xmldoc.DocumentElement
            Dim page As XmlElement = xmldoc.CreateElement("image")

            Dim ID As XmlElement = xmldoc.CreateElement("ID")
            Dim textid As XmlText = xmldoc.CreateTextNode("")
            textid.Value = max.ToString

            Dim imagename As XmlElement = xmldoc.CreateElement("imagename")
            Dim textImageName As XmlText = xmldoc.CreateTextNode("")
            textImageName.Value = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName)

            Dim URL As XmlElement = xmldoc.CreateElement("URL")
            Dim textURL As XmlText = xmldoc.CreateTextNode("")
            textURL.Value = imgpath

            Dim CreateDate As XmlElement = xmldoc.CreateElement("CreateDate")
            Dim textCreateDate As XmlText = xmldoc.CreateTextNode("")
            textCreateDate.Value = DateTime.Now.ToString().Trim()

            root.AppendChild(page)
            page.AppendChild(ID)
            ID.AppendChild(textid)

            page.AppendChild(imagename)
            imagename.AppendChild(textImageName)

            page.AppendChild(URL)
            URL.AppendChild(textURL)

            page.AppendChild(CreateDate)
            CreateDate.AppendChild(textCreateDate)
            xmldoc.Save(xmlpath)
        End If
    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim ds As New dataset
        ds.ReadXml(Server.MapPath("imagefile.xml"))
        imggrid.DataSource = ds
        imggrid.Columns(0).Visible = True
        imggrid.DataBind()
        imggrid.Columns(0).Visible = False
    End Sub


    Protected Sub imggrid_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles imggrid.RowDeleting
        Dim id As String
        xmlpath = Server.MapPath("~/imagefile.xml")
        id = imggrid.DataKeys(e.RowIndex).Value.ToString
        Dim xmldoc As New XmlDocument
        xmldoc.Load(xmlpath)
        Dim child As XmlNode = xmldoc.SelectSingleNode("picture/image[ID='" & id & "']")
        xmldoc.DocumentElement.RemoveChild(child)
        xmldoc.Save(xmlpath)

    End Sub


So when upload a image that image information should be added in xml file look below…
<?xml version="1.0" encoding="utf-8"?>
<picture>

<image>
    <ID>2</ID>
    <imagename>73546383.jpg</imagename>
    <URL>~/image/73546383.jpg</URL>
    <CreateDate>16-07-2011 1:24:17 PM</CreateDate>
  </image>
</picture>
I hope  this one will be helpful to all………

0 comments:

Post a Comment