• 0

VB.Net, text file operations help


Question

Hello,

Here is the scenario, let's say I have the following XML file:

<updates>

<update date="2-2-2002" version="0.1" file="Updates\0.1.xml"></update>

<update date="5-5-2002" version="0.2" file="Updates\0.2.xml"></update>

<update date="6-5-2002" version="0.3" file="Updates\0.3.xml"></update>

<update date="6-5-2002" version="0.4" file="Updates\0.4.xml"></update>

<update date="6-5-2002" version="0.5" file="Updates\0.5.xml"></update>

</updates>

I want to generate a list of the "file" attribute contents from let's say 0.3 -> 0.5: ( I shall input 0.3,0.4,0.5 to the program I'm making, and it auto generates the content I need )

Updates\0.3.xml

Updates\0.4.xml

Updates\0.5.xml

What's the easiest way to do that ?

p.s: if i were to generate an updates list of all lines inside the xml file, it would be easy, but i'm looking for specific lines.

Link to comment
https://www.neowin.net/forum/topic/743768-vbnet-text-file-operations-help/
Share on other sites

10 answers to this question

Recommended Posts

  • 0

Hi,

.NET does provide a series of classes for dealing with xml. What you could do, is create a function which opens the xml document (either with a quick xml reader, or with an xml document, which allows you to query the xml using xpath) and produces a list of string items:

Public Function GetFileList(ByVal xmlSourceFile As String, ByVal rangeStart As Double, ByVal rangeEnd As Double) As List(Of String)
	Dim document As New Xml.XmlDocument()
	document.Load(xmlSourceFile)
	Dim list = New List(Of String)

	For Each node As Xml.XmlNode In document.SelectNodes("//updates/update")
		Dim version As Double
		If (Double.TryParse(node.Attributes("version").Value, version)) Then
			If (version &gt;= rangeStart And version &lt;= rangeEnd) Then
				list.Add(node.Attributes("file").Valuef
		End If
	Next

	list.Sort()
	Return list
End Function

  • 0

I'm having a little bit of a problem

Dim file As String = System.Windows.Forms.Application.StartupPath + "\tempupdates.xml"

		stringlist = determine.GetFileList(file, ok(0), ok(1))

after I pass the tempupdates.xml file through the function, I try to delete the file using code ( System.IO.File.Delete(System.Windows.Forms.Application.StartupPath + "\tempupdates.xml") ) , but it says that it can't delete it because the file is in use.

( Trying to delete it after the function exits )

Any idea?

Edit: Actually nevermind, I commented some other functions and the delete function is working now... I think some other function is keeping the file in use.

edit2: nevermind about this, all fixed.. StreamReaders wasn't closed at their right positions :o

Edited by murderdoll
  • 0

I'm not sure if this will fix the file-access problem, but it might.

Instead of assigning streamReaders like this:

Dim sr As IO.StreamReader = IO.File.OpenText(file2)

do this instead:

Dim sr As New StreamReader(New FileStream(file2, FileMode.Open))

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • I assume he would make the same statement in those cases as well.
    • Looks like an alien. Probably is an alien. First it was aaaaallll Azure and drop everything else. Now its aaaaallll AI and drop everything else. Narrow-minded. I really loathe this guy. He's good for shareholders but absolutely nothing else. Dry as desert sand and evil to the bone.
    • Don’t care about a wrestling union. No normal person should care. 
    • Limassol, Cyprus. Just south of Turkey. NOT Russia.
    • Hello, Given the reports of Chinese Mini PCs shipping with malware, I would recommend wiping the machine and performing a clean install of Windows on it before use.  From what I can infer from the reports, the Mini PCs that shipped with malware were not the result of targeted purposeful action on the part of the device manufacturers (which is something that has happened with low-cost Android smartphones and TV boxes from China) but rather due to lax security in the manufacturing process.   Getting back to the subject at hand, there are a few steps you will want to go through before wiping the Mini PC: You can start preparing even before the Mini PC arrives.  Once you have ordered it and know the brand and model, go to the manufacturer's website and download all of the latest device drivers, BIOS (UEFI) firmware updates, machine-specific software (if any), and manuals.  Many Mini PC manufacturers do not do a lot of customization of their device drivers, just shipping whatever device drivers the the silicon vendors provide.  I still recommend downloading them, though, just in case there are some customizations or for initial install since those are the drivers you know the manufacturer validated for the Mini PC.  Store these in a safe place, so you have them ready when the Mini PC arrives. Use Microsoft's Windows Media Creation Tool to create an installation USB.  You can also create a directory on installation USB--like C:\DRIVERS\ or whatnot--and store the extracted device drivers there in case you need them while or after installing Windows. Once the Mini PC arrives, and you have your Windows installation USB available, you can proceed with wiping the PC and doing the clean install.  Here's how you do that, step-by-step: Check the computer and make sure you know how to boot it from a USB flash drive (may be a specific key you have to press when the computer is powered on, or a change to the BIOS (UEFI) firmware settings.  The PC may tell you what key combination you need to press to boot from another drive, or the manual for the PC may it. Plug the USB flash drive into the computer and power it up using the means to have it boot from the Windows install USB. Once the computer finishes booting, it should be at a Windows installation screen. Do not agree to any prompts, copyright licenses, or click on any buttons. Press the Shift + F10 keys together to open a Command Prompt. Run DISKPART to start the command-line disk partitioning utility. The command line prompt will change to DISKPART>. At the DISKPART> prompt, type LIST DISK to get the numbers of all drives installed in the system. Make a note of what number is assigned to what drive (if the Mini PC has more than one drive).  At the DISKPART> prompt, type SEL DISK n  where n is the number of the drive containing Windows. At the DISKPART> prompt, type CLEAN and this will erase the GPT/MBR code from the beginning of the drive. *WARNING:* After performing the clean operation, the drive now be blank/erased, and everything on it will be gone (all files, etc.).  You can exit DiskPart and just continue with the Windows installation as you normally would.  If needed, you can install the device drivers you put on the Windows install media to get your network connection up and running, and from there run Windows Update to get the operating system and device drivers up to date Regards, Aryeh Goretsky
  • Recent Achievements

    • Week One Done
      cac1lll earned a badge
      Week One Done
    • One Month Later
      Falcon.ai earned a badge
      One Month Later
    • Week One Done
      Falcon.ai earned a badge
      Week One Done
    • Dedicated
      EYEREX earned a badge
      Dedicated
    • First Post
      Electronic Person earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      626
    2. 2
      ATLien_0
      238
    3. 3
      Xenon
      166
    4. 4
      neufuse
      142
    5. 5
      +FloatingFatMan
      123
  • Tell a friend

    Love Neowin? Tell a friend!