Process Data With in XML File

Process Data With in XML File




Download source files - 25 Kb



 
Introduction
This Article Illustrate how to process data within XML File. For this Article I have developed a small Application that allows users to: 
·        Read Data from XML file and display it in the ASP.NET DataGrid Control. 
·        Write Data in XML File. 
·        Updating Data in XML File. 
 
Getting Started 
There are 2 main files: 
1.    Products.xml  
2.    ItemXML.vb   
XML file (Products.xml) contains three elements 

ItemID ‘ functioning as a primary key
ProductName
Price
  <SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">
<o:p></o:p> <?xml version="1.0" standalone="yes"?>
<Items><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 

GetAll ( Return All the Items from XML File)
Insert
Update
GetByID (Its Takes ItemID return the requested item detail from XML File)
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. 
  Public Function GetAll(ByVal Path As String) As DataTable
            Dim dt As New DataTable
            Dim ds As New DataSet
            ds.ReadXml(Path &;amp; "/Products.xml")
            Dim dt_ret As DataTable = ds.Tables(0)
            Return dt_ret        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. 
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 &;amp; "/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 &;amp; "/Products.xml", XmlWriteMode.WriteSchema)
                Return 1
            Catch ex As Exception
                Return 0
            End Try
        End Function
Insert function takes 3 parameters (ProductName, Price, Path). 
Item ID will be generated automatically by incrementing the last item ID in XML file by using the code: 
 
dr("ItemID") = CType(ds.Tables(0).Rows(ds.Tables(0).Rows.Count - 1)("ItemID"), Long)  1 
 
ds.Tables(0).Rows.Count -1 gives the last index value of the DataSet object 
 
 
For Writing to the XML File, I am using WriteXml method of the DataSet that takes the path of the file as a Parameter. 
 
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. 
Updating Data in XML File

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. 
 
ObjXMLData.Update(Request.QueryString("Product_ID"), Txt_ProductName.Text, Txt_Price.Text, Server.MapPath("/").ToString)
  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 &;amp; "/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                        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 &;amp; "/Products.xml",
XmlWriteMode.WriteSchema)                    End If
                Next
                Return 1
            Catch ex As Exception
                Return 0
            End Try        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). 
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. 
For any kind of help contact me:
[url=mailto:arshadras@hotmail.com]arshadras@hotmail.com[/url]
 



About Arshad Raheed






Arshad Rasheed is working as an Asst. Project Manager in UniSolutionZ. He is a Microsoft Certified professional for Developing and Implementing Web Applications using VS.NET 2003, and working on .Net Technologies from last 2 years.


Click
here
to view Arshad Raheed's online profile.    (2007-4-18:09:23)

 感谢原创者的辛勤劳动,希望对您有所帮助,转载请注明原出处。
 您可能对 [XML] 的这些文章也感兴趣:

用XSLT生成网页菜单
XML介绍系列(7)
了解WEB页面工具语言XML(六)展望
用XML VBS在ASP中实现报表的打印
了解WEB页面工具语言XML(五)好处
利用XmlSerializer在测试环境判断两个对象值相等
xml中的空格之完全解说
亲密接触xml(2)---XML不做任何事情
XML在语音中的应用
企业JavaBean(EJB) 3.0 全新体验