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

Im trying to understand your project and a bit confused. Why would you want to extract data from the XML file and store in multiple flat text files? It would seem to me (without know more about your project) that it would be easier to keep everything in the xml file and its very easy to read/write to & from that file.

Im trying to understand your project and a bit confused. Why would you want to extract data from the XML file and store in multiple flat text files? It would seem to me (without know more about your project) that it would be easier to keep everything in the xml file and its very easy to read/write to & from that file.

Hi, sorry if i wasnt clear before.

example of whats in template.txt:
There was a man called $$$placeholder1$$$

example of whats in XML file:

<names>
<name>
<placeholder1>Joe Bloggs</placeholder1>
</name>

<name>
<placeholder1>Steve Brown</placeholder1>
</name>
</names>

So what i want is to create one output file (file1.txt) which will say:

There was a man called Joe Bloggs

And then i want another file (file2.txt) to say:

There was a man called Steve Brown

I hope this helps :). P.S. I know it might be a strange request but its what i need to do as its in my requirements spec.

Thanks

OK, this is the best ive been able to come up with. Banging my head against a brick wall right now lol. At current, my program replaces all placeholders in the template file. But I need to create another output file with the second group of placeholders in the xml file filling the template file. I have so far managed only to fill the placeholders in the template file, with the first child node of the second group of placeholders in the xml file.

XML file is as follows:

<?xml version="1.0" encoding="UTF-8" ?> 
- <biggroup>
- <groups>
  <placeholder1>AAAAAA</placeholder1> 
  <placeholder2>BBBBBB</placeholder2> 
  <placeholder3>CCCCCC</placeholder3> 
  <placeholder4>DDDDDD</placeholder4> 
  <placeholder5>EEEEEE</placeholder5> 
- <!-- ><placeholder6>FFFFFF</placeholder6> IMAGINE THIS IS A MISSING PLACEHOLDER <!
  --> 
  </groups>
- <groups>
  <placeholder1>111111</placeholder1> 
  <placeholder2>222222</placeholder2> 
  <placeholder3>333333</placeholder3> 
  <placeholder4>444444</placeholder4> 
  <placeholder5>555555</placeholder5> 
  <placeholder6>666666</placeholder6> 
  </groups>
  </biggroup>
Imports System.IO
Imports System.Text.RegularExpressions

Imports System.Xml.XPath

Module Module1
    Private doc As Xml.XmlDocument

    Private Function evalFunction3(ByVal m As Match) As String
        Dim xpathDoc As XPathDocument
        Dim xmlNav As XPathNavigator
        Dim xmlNI As XPathNodeIterator
        xpathDoc = New XPathDocument("c:\data_file3.xml")
        xmlNav = xpathDoc.CreateNavigator()
        xmlNI = xmlNav.Select("/biggroup/groups/placeholder1")

        While (xmlNI.MoveNext())

        End While
        Return xmlNI.Current.Value
    End Function


    'Look through each node in the XML file
    Private Function evalFunction(ByVal m As Match) As String
        Dim node As Xml.XmlNode = doc.SelectSingleNode("biggroup/groups/" & 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_file3.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()


            'writing to a second output file
            Dim temporarytext2 As String = Regex.Replace _
                (IO.File.ReadAllText(template_path), "\${3}(?<name>.+?)\${3}", AddressOf evalFunction3)

            Dim objReader2 As New StreamWriter("c:\output2.txt")
            objReader2.Write(temporarytext2)
            objReader2.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

Any help would be appreciated greatly, I have been trying hard to complete this myself.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.