943,545 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 3212
  • VB.NET RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Sep 18th, 2009
0

Re: Need Help TXT to XML Converter

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.
Attached Files
File Type: txt example.txt (37.8 KB, 21 views)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ktkevin1222 is offline Offline
10 posts
since Sep 2009
Sep 19th, 2009
0

Re: Need Help TXT to XML Converter

VB.NET Syntax (Toggle Plain Text)
  1. Imports System.IO
  2. Imports System.Text
  3.  
  4. Public Class frmReadFile
  5.  
  6.  
  7. Private Shared Sub TxtToXML(ByVal inFile As String, ByVal outFile As String)
  8. Dim sr As New StreamReader(inFile)
  9.  
  10. Dim sb As New StringBuilder()
  11.  
  12. Do While sr.Peek() >= 0
  13. Dim line As String = sr.ReadLine()
  14. Dim fields() As String = line.Split(New [Char]() {","c}, System.StringSplitOptions.None)
  15.  
  16. sb.AppendLine("<CuePoint>")
  17. sb.AppendLine(" <Time>" & fields(0).Trim() & "</Time>")
  18. sb.AppendLine(" <Type>event</Type>")
  19. sb.AppendLine(" <Name>Marker " & fields(1).Trim() & "</Name>")
  20. sb.AppendLine(" <Parameters>")
  21. sb.AppendLine(" <Parameter>")
  22. sb.AppendLine(" <Name />")
  23. sb.AppendLine(" <Value>" & fields(2).Trim() & "</Value>")
  24. sb.AppendLine(" </Parameter>")
  25. sb.AppendLine(" </Parameters>")
  26. sb.AppendLine("</CuePoint>")
  27. Loop
  28.  
  29. sr.Close()
  30. sr.Dispose()
  31.  
  32. Dim result As String = sb.ToString()
  33. If (File.Exists(outFile)) Then
  34. File.Delete(outFile)
  35. End If
  36. File.WriteAllText(outFile, sb.ToString())
  37. End Sub
  38.  
  39. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  40. 'This is since I don't have your file. Change this to the path on YOUR machine
  41. TxtToXML("C:\dotnetwin\example.txt", "C:\dotnetwin\example.xml")
  42. MessageBox.Show("Done")
  43. End Sub
  44. End Class

It ran in under a second. Doesn't this do what you want?
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 19th, 2009
0

Re: Need Help TXT to XML Converter

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.
Last edited by ktkevin1222; Sep 19th, 2009 at 6:03 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ktkevin1222 is offline Offline
10 posts
since Sep 2009
Sep 19th, 2009
0

Re: Need Help TXT to XML Converter

decimal.Parse("1.3107711").ToString("F4") should do the trick
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 19th, 2009
0

Re: Need Help TXT to XML Converter

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ktkevin1222 is offline Offline
10 posts
since Sep 2009
Sep 19th, 2009
0

Re: Need Help TXT to XML Converter

VB.NET Syntax (Toggle Plain Text)
  1. sb.AppendLine(" <Time>" & decimal.Parse(fields(0).Trim()).ToString("F4") & "</Time>")
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 19th, 2009
0

Re: Need Help TXT to XML Converter

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.
Last edited by ktkevin1222; Sep 19th, 2009 at 9:47 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ktkevin1222 is offline Offline
10 posts
since Sep 2009
Sep 20th, 2009
0

Re: Need Help TXT to XML Converter

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

If so:
VB.NET Syntax (Toggle Plain Text)
  1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  2. Dim lst As New List(Of String)
  3. lst.Add("52.000000")
  4. lst.Add("160.000000")
  5. lst.Add("271.000000")
  6. For Each s In lst
  7. Dim i1 As Integer = GetInteger(Decimal.Parse(s))
  8. Console.WriteLine("Original Value: {0}", s)
  9. Console.WriteLine("New Value: {0:F0}", i1)
  10. Next
  11. End Sub
  12.  
  13. Private Shared Function GetInteger(ByVal someDecimal As Decimal) As Integer
  14. If (someDecimal <= 0) Then
  15. Return 0
  16. End If
  17. Return Convert.ToInt32(Math.Floor(someDecimal / 100))
  18. End Function

Results in:
text Syntax (Toggle Plain Text)
  1. Original Value: 52.000000
  2. New Value: 0
  3. Original Value: 160.000000
  4. New Value: 1
  5. Original Value: 271.000000
  6. New Value: 2
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 20th, 2009
0

Re: Need Help TXT to XML Converter

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.
Last edited by ktkevin1222; Sep 20th, 2009 at 12:05 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ktkevin1222 is offline Offline
10 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Help with the FormClosing() Event
Next Thread in VB.NET Forum Timeline: .Net Framework





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC