9:00 PM

Reading and writing XML data in asp.net

Posted by vijay


Introduction:

It’s very interesting when write about XML  an extensible markup language…

Here you are going to see different way to read and write the XML data.
Now I share the simple and easy methods to know about xml read and write.
Its basic question initially to ask about an xml everywhere

What is XML and why use xml?

·         Xml is the universal format for describing and exchange the data.
·         Xml  is self describing and easy to work
·         Xml described how the data is structured.

Read XML Data
By using dataset we can easily read the xml data and easy bind the data in gridview or in dataset
  Dim ds As New DataSet
        ds.ReadXml(Server.MapPath("prod.xml"))

Once we get the data to dataset we can manipulate the data to our needs..
For Each dr As DataRow In ds.Tables(0).Rows
      
 Next

Once the data has set in dataset then we easily bind the data into gridview or in dataset
        grddet.DataSource = ds
        grddet.DataBind()

Write XML data
By using dataset this is way to write XML data.
Dim sqlda As New SqlDataAdapter("select * from products", sqlcon)
        sqlda.Fill(ds, "product")
        ds.DataSetName = "Newproducts"
        ds.WriteXml(Server.MapPath("prod1.xml"))

 Also using xmldocument we can get the data from dataset and we do manipulate or bind the data if the conditions needed..
Dim xmldoc As New XmlDataDocument(ds)
        grddet.DataSource = xmldoc.DataSet
        grddet.DataBind()


For doing manipulation
'Manupulate
        Dim xele As XmlElement
        xmldoc.GetElementsByTagName("products")
        For Each xele In xmldoc.ChildNodes
            If xele.Name = "car" Then
                xele.Value = "100"
            End If
        Next


Conclusion:
From this article you have seen how to read and write XML ,I hope you learn something from this keep reading happy coding

5:37 AM

Insert/edit/delete in Xml using Asp.net

Posted by vijay


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………