I'm fairly new to coding VB 2008

I need help converting a text document with a type of code in it. One line looks like this: 52.000000, 400.000000, 1.3107711, 0

I need to convert that to an xml file that looks like this:
- <CuePoint>
<Time>11619</Time>
<Type>event</Type>
<Name>Marker 01</Name>
- <Parameters>
- <Parameter>
<Name />
<Value>0</Value>
</Parameter>
</Parameters>
</CuePoint>

The time in the text is 1.310771
value in text is 52.000000

I have hundreds of lines like that that need converting so I need something that will make the process automated.

I looked at other posts and was unable to convert the the code to what I needed it to do because of my lack of coding knowledge. Please help.

Did you copy and paste your XML example from internet explorer? IE adds those leading dashes to XML nodes -- but parses don't recognize them.

I tried with both firefox and internet explorer they both add the leading dash. I see that wordpad doesn't add the dashes.

Well I don't see how the lines relate to the XML sample you posted. None of the values match up. Is this intended or are they different examples? If it wasn't intended then post a matching example and we'll go from there.

this was intended. The xml value 0 is equivalent to the text value 52.000000, xml value 1 is text value 152.000000, xml value 2 is text value 271.000000. also the time in the xml is (example) 1634 which is equivalent to 1.634 seconds and in the text is has 6 decimal places. There are only three values and then time is really specific in this case.

I still don't understand your formatting but you can format it however you want:

Imports System.IO
Imports System.Text

Public Class frmReadFile
	Private Shared Sub CreateSampleFile()
		Dim sb As New StringBuilder()
		sb.AppendLine("52.000000, 400.000000, 1.3107711, 0")
		sb.AppendLine("51.000000, 401.000000, 2.3107711, 0")
		sb.AppendLine("55.000000, 402.000000, 3.3107711, 0")
		Dim buffer() As Byte = ASCIIEncoding.UTF8.GetBytes(sb.ToString())
		Dim fs As New FileStream("C:\testData.txt", FileMode.Create)
		fs.Write(buffer, 0, buffer.Length)
		fs.Close()
		fs.Dispose()
	End Sub


	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

		CreateSampleFile()

		Dim sr As New StreamReader("C:\testData.txt")

		Dim sb As New StringBuilder()

		Do While sr.Peek() >= 0
			Dim line As String = sr.ReadLine()
			Dim fields() As String = line.Split(New [Char]() {","c}, System.StringSplitOptions.None)

			sb.AppendLine("<CuePoint>")
			sb.AppendLine("	<Time>" & fields(0) & "</Time>")
			sb.AppendLine("	<Type>event</Type>")
			sb.AppendLine("	<Name>Marker " & fields(1) & "</Name>")
			sb.AppendLine("	<Parameters>")
			sb.AppendLine("		<Parameter>")
			sb.AppendLine("			<Name />")
			sb.AppendLine("			<Value>" & fields(2) & "</Value>")
			sb.AppendLine("		</Parameter>")
			sb.AppendLine("	</Parameters>")
			sb.AppendLine("</CuePoint>")
		Loop

		sr.Close()
		sr.Dispose()

		Dim result As String = sb.ToString()
		Console.WriteLine(result)
	End Sub
End Class

Results in:

<CuePoint>
	<Time>52.000000</Time>
	<Type>event</Type>
	<Name>Marker  400.000000</Name>
	<Parameters>
		<Parameter>
			<Name />
			<Value> 1.3107711</Value>
		</Parameter>
	</Parameters>
</CuePoint>
<CuePoint>
	<Time>51.000000</Time>
	<Type>event</Type>
	<Name>Marker  401.000000</Name>
	<Parameters>
		<Parameter>
			<Name />
			<Value> 2.3107711</Value>
		</Parameter>
	</Parameters>
</CuePoint>
<CuePoint>
	<Time>55.000000</Time>
	<Type>event</Type>
	<Name>Marker  402.000000</Name>
	<Parameters>
		<Parameter>
			<Name />
			<Value> 3.3107711</Value>
		</Parameter>
	</Parameters>
</CuePoint>

Ok I apologize I must have not been clear. Ok so I have the text file already since I noticed a new text document is created through the code, so no new text file i need to open an existing one and convert it. Also the text document is code itself that I need to convert to a different version as an xml (also I have a lot of these types of text documents)

The example code of the original text document I will have: 52.000000, 400.000000, 1.3107711, 0

52.000000 = the value from the text i need converted to xml under <value>0<value>

160.000000 = the value from the text i need converted to xml under <value>1<value>

271.000000 = the value from the text i need converted to xml under <value>2<value>

All the above values repeat and vary at random but they will not be anything other than what I listed above.

400.000000 = null object I need this to NOT be included in xml

1.3107711 = time that I will need converted from text to the xml in format xml format being <time>1310</time>. Basically its 1.310 without a decimal and 4 less decimal places (7711 taken out). If for example the time is 10.3476544 then it will come out as 10347 (10.347 without decimal)

XML format of the time will be <time>(insert time here)</time> and i have many random time values from 1 to the hundreds that I need converted so I need to define time as a variable not as a specific like 1.310771 since it can be anything from 1 to possibly 1000.

0 = null object I need this to NOT be included in xml

Also the marker value in the xml can be anything as long as it has the <marker>(insert anything here)</marker>

hope that cleared up what each value means and what they can be.

I know this is a lot to ask and this is my last resort after my friend started having technical difficulties with his computer. I'm sorry if this is much.

If you can still help and you need info please ask and also if you need an entire file to see exactly how many lines I'm talking about I can upload it here (I think).

I don't understand what you're asking. Are you saying you need to read the text file and match it up to an existing XML file and modify the contents?

Sorry. What I'm saying is that I have the text file that I need to edit since the code you gave creates a new text document. I need that text document converted into a new xml file in the format mentioned before.

Just modify the code. I had it create the text file since I didn't have your file on my machine.

Imports System.IO
Imports System.Text

Public Class frmReadFile

	Private Shared Sub CreateSampleFile(ByVal outFile As String)
		Dim sb As New StringBuilder()
		sb.AppendLine("52.000000, 400.000000, 1.3107711, 0")
		sb.AppendLine("51.000000, 401.000000, 2.3107711, 0")
		sb.AppendLine("55.000000, 402.000000, 3.3107711, 0")
		Dim buffer() As Byte = ASCIIEncoding.UTF8.GetBytes(sb.ToString())
		If (File.Exists(outFile)) Then
			File.Delete(outFile)
		End If

		Dim fs As New FileStream(outFile, FileMode.Create)
		fs.Write(buffer, 0, buffer.Length)
		fs.Close()
		fs.Dispose()
	End Sub

	Private Shared Sub TxtToXML(ByVal inFile As String, ByVal outFile As String)
		Dim sr As New StreamReader(inFile)

		Dim sb As New StringBuilder()

		Do While sr.Peek() >= 0
			Dim line As String = sr.ReadLine()
			Dim fields() As String = line.Split(New [Char]() {","c}, System.StringSplitOptions.None)

			sb.AppendLine("<CuePoint>")
			sb.AppendLine("	<Time>" & fields(0).Trim() & "</Time>")
			sb.AppendLine("	<Type>event</Type>")
			sb.AppendLine("	<Name>Marker " & fields(1).Trim() & "</Name>")
			sb.AppendLine("	<Parameters>")
			sb.AppendLine("		<Parameter>")
			sb.AppendLine("			<Name />")
			sb.AppendLine("			<Value>" & fields(2).Trim() & "</Value>")
			sb.AppendLine("		</Parameter>")
			sb.AppendLine("	</Parameters>")
			sb.AppendLine("</CuePoint>")
		Loop

		sr.Close()
		sr.Dispose()

		Dim result As String = sb.ToString()
		If (File.Exists(outFile)) Then
			File.Delete(outFile)
		End If
		File.WriteAllText(outFile, sb.ToString())
	End Sub

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		'This is since I don't have your file. You dont run this line since you already have the file.
		CreateSampleFile("C:\testdata.txt")
		'This is the real work
		TxtToXML("C:\testdata.txt", "C:\testdata.xml")
	End Sub
End Class

Thing is my time varies in each line and I have hundreds of lines and lots of files. I uploaded an example so you can see exactly how many lines the file contains. Thank you for your support so far! I will add rep to you most likely when we have this worked out.

Imports System.IO
Imports System.Text

Public Class frmReadFile


	Private Shared Sub TxtToXML(ByVal inFile As String, ByVal outFile As String)
		Dim sr As New StreamReader(inFile)

		Dim sb As New StringBuilder()

		Do While sr.Peek() >= 0
			Dim line As String = sr.ReadLine()
			Dim fields() As String = line.Split(New [Char]() {","c}, System.StringSplitOptions.None)

			sb.AppendLine("<CuePoint>")
			sb.AppendLine("	<Time>" & fields(0).Trim() & "</Time>")
			sb.AppendLine("	<Type>event</Type>")
			sb.AppendLine("	<Name>Marker " & fields(1).Trim() & "</Name>")
			sb.AppendLine("	<Parameters>")
			sb.AppendLine("		<Parameter>")
			sb.AppendLine("			<Name />")
			sb.AppendLine("			<Value>" & fields(2).Trim() & "</Value>")
			sb.AppendLine("		</Parameter>")
			sb.AppendLine("	</Parameters>")
			sb.AppendLine("</CuePoint>")
		Loop

		sr.Close()
		sr.Dispose()

		Dim result As String = sb.ToString()
		If (File.Exists(outFile)) Then
			File.Delete(outFile)
		End If
		File.WriteAllText(outFile, sb.ToString())
	End Sub

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		'This is since I don't have your file. Change this to the path on YOUR machine
		TxtToXML("C:\dotnetwin\example.txt", "C:\dotnetwin\example.xml")
		MessageBox.Show("Done")
	End Sub
End Class

It ran in under a second. Doesn't this do what you want?

Thank you so much! (had to fix one thing in script and that was switch fields 0 and 2) I was looking to find a command that did what (& fields(0).Trim() &) does.

One last thing. During the conversion is it possible to make the thing change a line such as (" <Time>" & fields(2).Trim() & "</Time>") to go from 1.3107711 to 1310 (cut off 7711 and take out decimal)? I need this done for each time value.

I know what i typed may look confusing so if need be i can clarify.

decimal.Parse("1.3107711").ToString("F4") should do the trick

Ok I understand what decimal.Parse will do and the i believe (F4) will only leave 4 numbers but is there anyway i can do this to every time value?

sb.AppendLine("	<Time>" & decimal.Parse(fields(0).Trim()).ToString("F4") & "</Time>")

That is almost what Im looking for. this last part I couldn't figure out. I need to have the same format except also get rid of the decimal. It seems obvious but i can't figure it out.

Also I need to convert 52.000000 to 0, 160.000000 to 1, and 271.000000 to 2. Haven't been able to do a convert or replace command correctly.

How are you doing the math here?
0-99 = 0
100-199 = 1
200-299 = 2 ?

If so:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
		Dim lst As New List(Of String)
		lst.Add("52.000000")
		lst.Add("160.000000")
		lst.Add("271.000000")
		For Each s In lst
			Dim i1 As Integer = GetInteger(Decimal.Parse(s))
			Console.WriteLine("Original Value: {0}", s)
			Console.WriteLine("New Value: {0:F0}", i1)
		Next
	End Sub

	Private Shared Function GetInteger(ByVal someDecimal As Decimal) As Integer
		If (someDecimal <= 0) Then
			Return 0
		End If
		Return Convert.ToInt32(Math.Floor(someDecimal / 100))
	End Function

Results in:

Original Value: 52.000000
New Value: 0
Original Value: 160.000000
New Value: 1
Original Value: 271.000000
New Value: 2

well i don't know about the math that's just how the code comes out. I need the xml to have the replaced values though and I need the time to not have the decimal.

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.