Click here to Skip to main content
15,881,882 members
Articles / Web Development / ASP.NET
Article

Process Data With in XML File

Rate me:
Please Sign up or sign in to vote.
2.67/5 (5 votes)
28 Aug 20062 min read 72.9K   1.2K   35   2
Process Data With in XML File

Sample Image - WritingDataInXMLFile.jpg

Introduction

This Article Illustrate how to process data within XML File. For this Article I have developed a small Application that allows users to: <o:p>

·         Read Data from XML file and display it in the ASP.NET DataGrid Control.<o:p>

·         Write Data in XML File.<o:p>

·         Updating Data in XML File.<o:p>

 <o:p>

Getting Started<o:p>

There are 2 main files:<o:p>

1.     Products.xml <o:p>

2.     ItemXML.vb  <o:p>

XML file (Products.xml) contains three elements <o:p>

  1. ItemID ‘ functioning as a primary key<o:p>
  2. ProductName<o:p>
  3. Price

<o:p> 

<SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><o:p></o:p> <?xml version="1.0" standalone="yes"?>
<Items>
XML
<Item>
    <ItemID>1</ItemID>
    <ProductName>8-23 hours </ProductName>
    <Price>99</Price>
  </Item>
  <Item>
    <ItemID>2</ItemID>
    <ProductName>1 day</ProductName>
    <Price>92</Price>
  </Item>
</Items>

ItemXML.vb contains all the business logic that I have used to process XML File<o:p>

  1. GetAll ( Return All the Items from XML File)<o:p>
  2. Insert <o:p>
  3. Update<o:p>
  4. GetByID (Its Takes ItemID return the requested item detail from XML File)<o:p>

Reading Data from XML file and Binding it with DataGrid

 

I have used DataSet Object’s ReadXML method to Read data from XML file and then bind it to DataGrid control.

<o:p> 

VB.NET
Public Function GetAll(ByVal Path As String) As DataTable
            Dim dt As New DataTable
            Dim ds As New DataSet
            ds.ReadXml(Path & "/Products.xml")
            Dim dt_ret As DataTable = ds.Tables(0) 
            Return dt_ret
VB.NET
        End Function
Dim ObjXMLData as New BusinessLayer.ItemXML
            dt = ObjXMLData.GetAll(Server.MapPath("/").ToString())
            DataGrid1.DataSource = dt
            DataGrid1.DataBind()

Variable ‘Path’ contains Server.MapPath(“/”) .. The Server.MapPath is not Accessible from the Class file, that is why I have sent it from the code behind file.<o:p>

Inserting Data in XML File

Public Function Insert(ByVal ProductName As String, ByVal Price As Long, ByVal Path As String) As Long
           Try
               Dim dr As DataRow
               Dim ds As New DataSet
               ds.ReadXml(Path & "/Products.xml")
        dr = ds.Tables(0).NewRow
        dr("ItemID") = CType(ds.Tables(0).Rows(ds.Tables(0).Rows.Count - 1)("ItemID"), Long) + 1
        dr("ProductName") = ProductName
        dr("Price") = Price
        ds.Tables(0).Rows.Add(dr)
        ds.WriteXml(Path & "/Products.xml", XmlWriteMode.WriteSchema)
        Return 1
    Catch ex As Exception
        Return 0
    End Try
End Function

Insert function takes 3 parameters (ProductName, Price, Path).<o:p>

Item ID will be generated automatically by incrementing the last item ID in XML file by using the code:<o:p>

<o:p> 

dr("ItemID") = CType(ds.Tables(0).Rows(ds.Tables(0).Rows.Count - 1)("ItemID"), Long) + 1<o:p>

<o:p> 

ds.Tables(0).Rows.Count -1 gives the last index value of the DataSet object<o:p>

<o:p> 

<o:p> 

For Writing to the XML File, I am using WriteXml method of the DataSet that takes the path of the file as a Parameter.<o:p>

<o:p> 

Note: set permissions on the directory, if you do NOT want to make ALL files writeable, you could probably get away with setting the permissions on the Products.xml file itself.<o:p>

Updating Data in XML File

 

<o:p>

To achieve the updating process, I have used quite similar process as I have done in the Insertion. Here I am getting the Item ID from the QueryString and then compared it with all the ItemID’s from the XML file. If the ItemID exists, insert the new row at the same index value.<o:p>

<o:p> 

ObjXMLData.Update(Request.QueryString("Product_ID"), Txt_ProductName.Text, Txt_Price.Text, Server.MapPath("/").ToString)

VB.NET
Public Function Update(ByVal Item_ID As Long, ByVal ProductName As String, ByVal Price As Long, ByVal Path As String) As Long
           Try
               Dim ds As New DataSet
               ds.ReadXml(Path & "/Products.xml")
               Dim dr As DataRow
               dr = ds.Tables(0).NewRow
               Dim a As Integer
               Dim b As Integer
               For b = 0 To ds.Tables(0).Rows.Count - 1
                   If Item_ID = ds.Tables(0).Rows(b)("ItemID") Then
VB.NET
ds.Tables(0).Rows(b)("ItemID") = Item_ID
ds.Tables(0).Rows(b)("ProductName") = ProductName
ds.Tables(0).Rows(b)("Price") = Price
ds.WriteXml(Path & "/Products.xml", XmlWriteMode.WriteSchema)
VB.NET
        End If
    Next
    Return 1
Catch ex As Exception
    Return 0
End Try
VB.NET
End Function

That’s all for this little topic. The code snippet used above is just to explain and is not complete. Download the complete source code from the zip file (download link is on the top).<o:p>

I am not used to of writing Articles infect this is first Article and u could have some problems understanding what I have done so far.<o:p>

For any kind of help contact me: arshadras@hotmail.com<o:p>


License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer
Pakistan Pakistan
Arshad Rasheed is working as a Software Developer at Hambra Consulting. He is a Microsoft Certified professional for Developing and Implementing Web Applications using VS.NET 2003, and working on .Net Technologies from last 3 years.

Comments and Discussions

 
GeneralQuestion Pin
hp916-Apr-07 0:26
hp916-Apr-07 0:26 
GeneralExcellent example Pin
Gabriel Cassalho15-Apr-07 15:38
Gabriel Cassalho15-Apr-07 15:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.