Solved

XML File

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 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