Question Help with a while loop

judge

Member
Joined
Dec 7, 2009
Messages
18
Programming Experience
Beginner
Ok my code:

VB.NET:
Imports System.IO
Imports System.Text.RegularExpressions
Imports System.Xml

Module Module1

    Private Sub ReadXML(ByVal xnod As XmlNode)
        Dim xnodWorking As XmlNode
        Dim strValue As String = CType(xnod.Value, String)
        Dim oFile As System.IO.File
        Dim oRead As System.IO.StreamReader
        oRead = oFile.OpenText("C:\template.txt")
        Dim LineIn As String
        Dim temporarytext As String

        If Len(strValue) > 0 Then
            While oRead.Peek <> -1
                temporarytext = ""
                LineIn = oRead.ReadLine() 'read line from template file
                Dim oWrite As System.IO.StreamWriter
                oWrite = oFile.CreateText("C:\newtesttempfile.txt") 'create new txt file
                oWrite.WriteLine(LineIn) 'write line from template file to txt file
                oWrite.Close()
                'replace the $ph$'s with xnod.value
                temporarytext = Regex.Replace _
            (IO.File.ReadAllText("c:\newtesttempfile.txt"), "\${3}(?<name>.+?)\${3}", [B][U][SIZE="4"][COLOR="Red"]xnod.Value[/COLOR][/SIZE][/U][/B]) 'the xnod.value is not changing - need to get it to change
                Dim oWrite2 As IO.TextWriter
                oWrite2 = IO.File.AppendText("c:\output.txt")
                Console.WriteLine(temporarytext)
                oWrite2.WriteLine(temporarytext)
                oWrite2.Close()


            End While
        End If

        If xnod.HasChildNodes Then
            xnodWorking = xnod.FirstChild
            Do Until IsNothing(xnodWorking)
                ReadXML(xnodWorking)
                xnodWorking = xnodWorking.NextSibling
            Loop
        End If
    End Sub

    Sub Main()
        Dim xmlData As New XmlDocument
        xmlData.Load("c:\data_file3.xml")
        Dim xnod As XmlNode = xmlData.DocumentElement
        ReadXML(xnod)
        Console.ReadKey()

    End Sub
End Module

What im trying to do is to change the xnod.value so that each time the loop occurs, it adds one to the loop. Sounds easy, but a number of things have stopped me from doing this so far.
 
Last edited by a moderator:
I'm not sure I understand what you're trying to achieve. At no point inside your While loop are you assigning anything to xnod or xnod.Value so I wouldn't expect it to change. I think you need to provide a more complete explanation.
 
Hi there, sorry. I am trying to loop so that everytime the loop goes round i want to move to the next sibling of the node. However if i put the xnodWorking = xnodWorking.NextSibling inside the loop it is not using the instance of an object. There is probably a really easy solution to this, but i just cant see it.

So what im trying to achieve:

Sub Main()
'Load xml file
'Call the ReadXML sub

ReadXML Sub()
'Read line by line from template file (.txt)
'Replace the placeholders inside the template file, with the xnod.Value.
'Write the newly formed text to the console, and append to the object writer (output.txt)
'Loop again, and change the xnod.value to the next sibling.

... I know what i want to do, but just having trouble implementing it, as i seem to get an error/ program doesnt work each time
 
Last edited:
Back
Top