Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'The way to read XML file and save data from XML file to CSV file or text file
- Imports System.Xml
- Imports System.IO
- Sub ....
- Dim xmlDoc As New XmlDocument()
- xmlDoc.Load("path/to/xml/file.xml")
- Dim nodes As XmlNodeList = xmlDoc.SelectNodes("/root/node")
- Dim sw As New StreamWriter("path/to/csv/file.csv") ' Also can use fille path of text file
- For Each node As XmlNode In nodes
- Dim values As String = ""
- For Each childNode As XmlNode In node.ChildNodes
- values += childNode.InnerText & ","
- Next
- sw.WriteLine(values.TrimEnd(","))
- Next
- sw.Close()
- End Sub
- 'Convert XML file to a two-dimentional array
- Private Sub btnXML_Click(sender As Object, e As EventArgs) Handles btnXML.Click
- ' Load the XML file into an XDocument object
- Dim doc As XDocument = XDocument.Load("your XML file")
- Dim table As New DataTable()
- ' Get a list of all the <entry> elements in the XML file
- Dim entries As List(Of XElement) = doc.Root.Elements("entry").ToList()
- ' Create a new DataGridView control
- Dim dataGridView As New DataGridView()
- ' Set the data source of the DataGridView control to a DataTable
- table.Columns.Add("Name", GetType(String))
- table.Columns.Add("Score", GetType(String))
- For Each entry As XElement In entries
- Dim name As String = entry.Element("Name").Value
- Dim score As String = entry.Element("Score").Value
- table.Rows.Add(name, score)
- Next
- dataGridView.DataSource = table
- ' Add the DataGridView control to a new form
- Dim form As New Form()
- form.Controls.Add(dataGridView)
- ' Show the form as a dialog box
- form.ShowDialog()
- End Sub
- 'fast researching technique
- Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
- 'Assume dt Is a DataTable object with data in it
- Dim searchValue As String = InputBox("Enter search value:", "Search")
- ' Split the items in the ListBox into an array and search for the keyword
- Dim eList = From item In lstHiScores.Items.Cast(Of String)() Where item.Split(","c).Contains(searchValue.Trim()) Select item
- ' Check if any items were found
- If eList.Count() > 0 Then
- ' Create a new window to show the matching items
- Dim resultWindow As New Form()
- Dim listBox As New ListBox()
- ' Add the matching items to the listbox
- For Each item In eList
- listBox.Items.Add(item)
- Next
- ' Add the listbox to the window and show it
- resultWindow.Controls.Add(listBox)
- resultWindow.Show()
- Else
- MessageBox.Show("No matching items found.")
- End If
- End Sub
- Sub SaveCsvToXml(csvPath As String, xmlPath As String)
- ' Load CSV file
- Dim lines As String() = System.IO.File.ReadAllLines(csvPath)
- ' Create new XML document
- Dim xmlDoc As New XmlDocument()
- Dim rootNode As XmlNode = xmlDoc.CreateElement("data")
- xmlDoc.AppendChild(rootNode)
- ' Loop through CSV lines and add elements to XML document
- For Each line As String In lines
- Dim values As String() = line.Split(",")
- Dim itemNode As XmlNode = xmlDoc.CreateElement("item")
- For i As Integer = 0 To values.Length - 1
- Dim elementNode As XmlNode = xmlDoc.CreateElement("element" & i)
- elementNode.InnerText = values(i)
- itemNode.AppendChild(elementNode)
- Next
- rootNode.AppendChild(itemNode)
- Next
- ' Save XML document to file
- xmlDoc.Save(xmlPath)
- End Sub
- Private Sub Btn_savcsv_Click(sender As Object, e As EventArgs) Handles Btn_savcsv.Click
- Dim HiScFile2 As StreamWriter = File.CreateText("Score.xml")
- Dim Splt As String
- HiScFile2.WriteLine("<?xml version=""1.0"" encoding=""UTF-8""?>")
- HiScFile2.WriteLine("<HiScores>")
- For Each Name As Object In lstHiScores.Items
- Splt = Name
- HiScFile2.WriteLine(" <entry>")
- Scoresplit = Split(Splt, ",")
- HiScFile2.WriteLine(" <Score>" + Scoresplit(0) + "</Score>")
- HiScFile2.WriteLine(" <Names>" + Scoresplit(1) + "</Names>")
- HiScFile2.WriteLine(" </entry>")
- Next
- <?xml version="1.0" encoding="UTF-8" ?>
- <dataset>
- <record>
- <Name>John</Name>
- <Score>40</Score>
- <Group>1</Group>
- </record>
- </dataset>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement