Solved

XML File

Posted on 2011-03-24
4
349 Views
Last Modified: 2012-05-11
I’m new to vb.NET and I’m creating a project with 5 tables, with the largest table having about 25 records, I would like to use XML.

I need some sample code showing how to
1.  Create the XML database
2.  Create the file structure
3.  Do Inserts
4.  Do Updates
5.  Do Deletes
6.  Do Searches

If anyone has any sample code I would appreciate it.
0
Comment
Question by:PhilChapmanJr
  • 2
  • 2
4 Comments
 
LVL 35

Expert Comment

by:Miguel Oz
ID: 35211093
0
 
LVL 2

Author Comment

by:PhilChapmanJr
ID: 35212511
The http://vb.net-informations.com/xml/vb.net-xml-tutorial.htm
has good examples of Createing and searching as XML File

Private Sub cmdCreateXML_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCreateXML.Click
        Dim writer As New XmlTextWriter("c:\product.xml", System.Text.Encoding.UTF8)

        writer.WriteStartDocument(True)
        writer.Formatting = Formatting.Indented
        writer.Indentation = 2
        writer.WriteStartElement("Table")
        createNode(1, "Product 1", "1000", writer)
        createNode(2, "Product 2", "2000", writer)
        createNode(3, "Product 3", "3000", writer)
        createNode(4, "Product 4", "4000", writer)
        writer.WriteEndElement()
        writer.WriteEndDocument()
        writer.Close()
    End Sub

Private Sub createNode(ByVal pID As String, ByVal pName As String, ByVal pPrice As String, ByVal writer As XmlTextWriter)

        writer.WriteStartElement("Product")
        writer.WriteStartElement("Product_id")
        writer.WriteString(pID)
        writer.WriteEndElement()
        writer.WriteStartElement("Product_name")
        writer.WriteString(pName)
        writer.WriteEndElement()
        writer.WriteStartElement("Product_price")
        writer.WriteString(pPrice)
        writer.WriteEndElement()
        writer.WriteEndElement()

    End Sub

Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click

        Dim xmlFile As XmlReader

        xmlFile = XmlReader.Create("c:\product.xml", New XmlReaderSettings())
        Dim ds As New DataSet
        Dim dv As DataView
        ds.ReadXml(xmlFile)

        dv = New DataView(ds.Tables(0))
        dv.Sort = "Product_Name"
        Dim index As Integer = dv.Find("Product 2")

        If index = -1 Then
            MsgBox("Item Not Found")
        Else
            MsgBox(dv(index)("Product_Name").ToString() & "  " & dv(index)("Product_Price").ToString())
        End If

    End Sub

But how would you
Insert a new record, Delete a Record and Update a Record


Example:
How would you insert 5, "Product 5", "5000"
and delete Item 3
and Update Item  2 from 2000 to 2050
0
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 500 total points
ID: 35212542
Creation code looks good, for searching use XPath. something like:
' Load the XmlDocument.
Dim xd As New XmlDocument()
xd.Load("C:\XmlTest.xml")
' Find the node where the Person's attribute ID is 1 using its XPath.
Dim nod As XmlNode = xd.SelectSingleNode("/Table/Product[@Product_Name='Product 2']")

the lilnk below contains a sample on how to search as well.

Note: Sorry forgot to add this link: (sample of add, delete, update under "Add to an XML File", "Delete an XML Node", "Edit an XML File")
http://www.vbforums.com/archive/index.php/t-478692.html
0
 
LVL 2

Author Closing Comment

by:PhilChapmanJr
ID: 35212640
Thanks again for the assistance
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now