• 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
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").Value)
			End If
		End If
	Next

	list.Sort()
	Return list
End Function

Link to comment
Share on other sites

  • 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
Link to comment
Share on other sites

  • 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))

Link to comment
Share on other sites

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

    • No registered users viewing this page.