What i am trying to do is create a conosle app in vb.net 2008. I have a XML file (data_file.xml), and a template file (template.txt). The template file contains a few sentences, each one containing a placeholder which needs to be filled. I need to create multiple output files (.txt), which contains the contents of the template file, with the placeholders filled with the values of the placeholders in the XMl file.
The XML file is at the moment:
<?xml version="1.0" ?>
- <CATALOG>
- <group>
<placeholder1>Nigel</placeholder1>
- <!-- ><placeholder2>andrew</placeholder2><!
-->
<placeholder3>colin</placeholder3>
<placeholder4>steve</placeholder4>
<placeholder5>daniel</placeholder5>
<placeholder6>dave</placeholder6>
</group>
- <group>
<placeholder1>richard</placeholder1>
<placeholder2>ryan</placeholder2>
<placeholder3>ronaldo</placeholder3>
<placeholder4>ryu</placeholder4>
- <!-- ><placeholder5>ragan</placeholder5><!
-->
<placeholder6>ronald</placeholder6>
</group>
</CATALOG>
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
As it stands, I can create one template file, containing the filled template contents using only the first group of placeholders. I am nearly there, just need a helping hand to finish the last piece of the jigsaw.
Thanks in advance, Judge6