Advertisement
Wontons

Jeev 2.0

Mar 16th, 2023 (edited)
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VisualBasic 4.57 KB | Source Code | 0 0
  1. 'The way to read XML file and save data from XML file to CSV file or text file
  2.  
  3. Imports System.Xml
  4. Imports System.IO
  5.  
  6. Sub ....
  7.     Dim xmlDoc As New XmlDocument()
  8.     xmlDoc.Load("path/to/xml/file.xml")
  9.     Dim nodes As XmlNodeList = xmlDoc.SelectNodes("/root/node")
  10.     Dim sw As New StreamWriter("path/to/csv/file.csv")                    ' Also can use fille path of text file
  11.    For Each node As XmlNode In nodes
  12.         Dim values As String = ""
  13.         For Each childNode As XmlNode In node.ChildNodes
  14.             values += childNode.InnerText & ","
  15.         Next
  16.         sw.WriteLine(values.TrimEnd(","))
  17.     Next
  18.     sw.Close()
  19. End Sub
  20.  
  21.  
  22.  
  23. 'Convert XML file to a two-dimentional array
  24.    Private Sub btnXML_Click(sender As Object, e As EventArgs) Handles btnXML.Click
  25.         ' Load the XML file into an XDocument object
  26.        Dim doc As XDocument = XDocument.Load("your XML file")
  27.         Dim table As New DataTable()
  28.         ' Get a list of all the <entry> elements in the XML file
  29.        Dim entries As List(Of XElement) = doc.Root.Elements("entry").ToList()
  30.  
  31.         ' Create a new DataGridView control
  32.        Dim dataGridView As New DataGridView()
  33.  
  34.         ' Set the data source of the DataGridView control to a DataTable
  35.  
  36.         table.Columns.Add("Name", GetType(String))
  37.         table.Columns.Add("Score", GetType(String))
  38.         For Each entry As XElement In entries
  39.             Dim name As String = entry.Element("Name").Value
  40.             Dim score As String = entry.Element("Score").Value
  41.             table.Rows.Add(name, score)
  42.         Next
  43.         dataGridView.DataSource = table
  44.  
  45.         ' Add the DataGridView control to a new form
  46.        Dim form As New Form()
  47.         form.Controls.Add(dataGridView)
  48.  
  49.         ' Show the form as a dialog box
  50.        form.ShowDialog()
  51.     End Sub
  52.  
  53. 'fast researching technique
  54.    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
  55.         'Assume dt Is a DataTable object with data in it
  56.        Dim searchValue As String = InputBox("Enter search value:", "Search")
  57.  
  58.         ' Split the items in the ListBox into an array and search for the keyword
  59.        Dim eList = From item In lstHiScores.Items.Cast(Of String)() Where item.Split(","c).Contains(searchValue.Trim()) Select item
  60.  
  61.         ' Check if any items were found
  62.        If eList.Count() > 0 Then
  63.             ' Create a new window to show the matching items
  64.            Dim resultWindow As New Form()
  65.             Dim listBox As New ListBox()
  66.  
  67.             ' Add the matching items to the listbox
  68.            For Each item In eList
  69.                 listBox.Items.Add(item)
  70.             Next
  71.  
  72.             ' Add the listbox to the window and show it
  73.            resultWindow.Controls.Add(listBox)
  74.             resultWindow.Show()
  75.         Else
  76.             MessageBox.Show("No matching items found.")
  77.         End If
  78.     End Sub
  79.  
  80. Sub SaveCsvToXml(csvPath As String, xmlPath As String)
  81.     ' Load CSV file
  82.    Dim lines As String() = System.IO.File.ReadAllLines(csvPath)
  83.  
  84.     ' Create new XML document
  85.    Dim xmlDoc As New XmlDocument()
  86.     Dim rootNode As XmlNode = xmlDoc.CreateElement("data")
  87.     xmlDoc.AppendChild(rootNode)
  88.  
  89.     ' Loop through CSV lines and add elements to XML document
  90.    For Each line As String In lines
  91.         Dim values As String() = line.Split(",")
  92.         Dim itemNode As XmlNode = xmlDoc.CreateElement("item")
  93.         For i As Integer = 0 To values.Length - 1
  94.             Dim elementNode As XmlNode = xmlDoc.CreateElement("element" & i)
  95.             elementNode.InnerText = values(i)
  96.             itemNode.AppendChild(elementNode)
  97.         Next
  98.         rootNode.AppendChild(itemNode)
  99.     Next
  100.  
  101.     ' Save XML document to file
  102.    xmlDoc.Save(xmlPath)
  103. End Sub
  104.  
  105. Private Sub Btn_savcsv_Click(sender As Object, e As EventArgs) Handles Btn_savcsv.Click
  106.         Dim HiScFile2 As StreamWriter = File.CreateText("Score.xml")
  107.         Dim Splt As String
  108.         HiScFile2.WriteLine("<?xml version=""1.0"" encoding=""UTF-8""?>")
  109.         HiScFile2.WriteLine("<HiScores>")
  110.         For Each Name As Object In lstHiScores.Items
  111.             Splt = Name
  112.             HiScFile2.WriteLine("  <entry>")
  113.             Scoresplit = Split(Splt, ",")
  114.             HiScFile2.WriteLine("    <Score>" + Scoresplit(0) + "</Score>")
  115.             HiScFile2.WriteLine("    <Names>" + Scoresplit(1) + "</Names>")
  116.             HiScFile2.WriteLine("  </entry>")
  117.         Next
  118.  
  119.  
  120. <?xml version="1.0" encoding="UTF-8" ?>
  121. <dataset>
  122.     <record>
  123.         <Name>John</Name>
  124.         <Score>40</Score>
  125.         <Group>1</Group>
  126.     </record>
  127. </dataset>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement