Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 357
  • Last Modified:

XML File

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
PhilChapmanJr
Asked:
PhilChapmanJr
  • 2
  • 2
1 Solution
 
PhilChapmanJrAuthor Commented:
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
 
Miguel OzSoftware EngineerCommented:
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
 
PhilChapmanJrAuthor Commented:
Thanks again for the assistance
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now