OK. As it stands:
Imports System.IO
Imports System.Text.RegularExpressions
Module Module1
Private doc As Xml.XmlDocument
'Look through each node in the XML file
Private Function evalFunction(ByVal m As Match) As String
Dim node As Xml.XmlNode = doc.SelectSingleNode("//" & m.Groups("name").Value)
If node Is Nothing Then
Return m.Value
Else
Return node.InnerText
End If
End Function
Sub Main()
Console.Title = "Text Replacement Tool"
Console.SetWindowSize(120, 60)
Try
doc = New Xml.XmlDocument
Console.WriteLine(vbNewLine & "Enter the path of the XML data file > ")
doc.Load("c:\data_file.xml") 'doc.Load(Console.ReadLine)
Console.WriteLine(vbNewLine & "Enter the path of the template text file > ")
Dim template_path As String = "c:\template.txt" 'Dim template_path As String = Console.ReadLine()
If File.Exists(template_path) = False Then
MsgBox("Could not find " & template_path & " ,check that the file exists ")
Main()
End If
Console.WriteLine(vbNewLine & "Enter the path of the output file you wish to create > ")
Dim output_path As String = "c:\output.txt" 'Dim output_path As String = Console.ReadLine()
Dim temporarytext As String = Regex.Replace _
(IO.File.ReadAllText(template_path), "\${3}(?<name>.+?)\${3}", AddressOf evalFunction)
Dim objReader As New StreamWriter(output_path)
objReader.Write(temporarytext)
objReader.Close()
'Count how many lines are in data assigned to temporarytext
Dim lines() As String = temporarytext.Split(New String() {vbNewLine}, StringSplitOptions.None)
For i As Integer = 0 To lines.Length - 1
'The current line text is assigned to the string variable line
Dim line As String = lines(i)
For Each m As Match In Regex.Matches(line, "\${3}placeholder[0-9]\${3}")
Console.WriteLine(vbNewLine & "Place holder missing in " & template_path & ": " & vbNewLine & "Name: " & m.Value _
& vbNewLine & "Line: " & i + 1 _
& vbNewLine & "Character position: " & m.Index & vbNewLine)
Console.ReadKey()
Next
Next
Console.WriteLine("Thankyou for using the Text Replacement Tool. Press any key to quit...")
Console.ReadKey()
Catch ex As Exception
MsgBox(ex.Message)
Main()
End Try
End Sub
End Module
Program runs great, no problems there. Only problem is that if my Data_file.xml has more than 1 group of data, the program catches an error "There are multiple root elements. Line 10, position2.". This refers to the position in my XML file where the second groups starts.
Any suggestions as to how i can fix this? I need to be able to read from different groups in the XML file, so that the program will create a new output file for each different groups data.
Thanks,
Judge