Solved

XML File

Posted on 2011-03-24
4
350 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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

832 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