Need Help TXT to XML Converter

Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2009
Posts: 10
Reputation: ktkevin1222 is an unknown quantity at this point 
Solved Threads: 0
ktkevin1222 ktkevin1222 is offline Offline
Newbie Poster

Need Help TXT to XML Converter

 
0
  #1
Sep 13th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,258
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 581
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Need Help TXT to XML Converter

 
0
  #2
Sep 13th, 2009
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.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 10
Reputation: ktkevin1222 is an unknown quantity at this point 
Solved Threads: 0
ktkevin1222 ktkevin1222 is offline Offline
Newbie Poster

Re: Need Help TXT to XML Converter

 
0
  #3
Sep 13th, 2009
I tried with both firefox and internet explorer they both add the leading dash. I see that wordpad doesn't add the dashes.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,258
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 581
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Need Help TXT to XML Converter

 
0
  #4
Sep 13th, 2009
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.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 10
Reputation: ktkevin1222 is an unknown quantity at this point 
Solved Threads: 0
ktkevin1222 ktkevin1222 is offline Offline
Newbie Poster

Re: Need Help TXT to XML Converter

 
0
  #5
Sep 13th, 2009
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.
Last edited by ktkevin1222; Sep 13th, 2009 at 9:18 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,258
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 581
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Need Help TXT to XML Converter

 
0
  #6
Sep 15th, 2009
I still don't understand your formatting but you can format it however you want:

  1. Imports System.IO
  2. Imports System.Text
  3.  
  4. Public Class frmReadFile
  5. Private Shared Sub CreateSampleFile()
  6. Dim sb As New StringBuilder()
  7. sb.AppendLine("52.000000, 400.000000, 1.3107711, 0")
  8. sb.AppendLine("51.000000, 401.000000, 2.3107711, 0")
  9. sb.AppendLine("55.000000, 402.000000, 3.3107711, 0")
  10. Dim buffer() As Byte = ASCIIEncoding.UTF8.GetBytes(sb.ToString())
  11. Dim fs As New FileStream("C:\testData.txt", FileMode.Create)
  12. fs.Write(buffer, 0, buffer.Length)
  13. fs.Close()
  14. fs.Dispose()
  15. End Sub
  16.  
  17.  
  18. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  19.  
  20. CreateSampleFile()
  21.  
  22. Dim sr As New StreamReader("C:\testData.txt")
  23.  
  24. Dim sb As New StringBuilder()
  25.  
  26. Do While sr.Peek() >= 0
  27. Dim line As String = sr.ReadLine()
  28. Dim fields() As String = line.Split(New [Char]() {","c}, System.StringSplitOptions.None)
  29.  
  30. sb.AppendLine("<CuePoint>")
  31. sb.AppendLine(" <Time>" & fields(0) & "</Time>")
  32. sb.AppendLine(" <Type>event</Type>")
  33. sb.AppendLine(" <Name>Marker " & fields(1) & "</Name>")
  34. sb.AppendLine(" <Parameters>")
  35. sb.AppendLine(" <Parameter>")
  36. sb.AppendLine(" <Name />")
  37. sb.AppendLine(" <Value>" & fields(2) & "</Value>")
  38. sb.AppendLine(" </Parameter>")
  39. sb.AppendLine(" </Parameters>")
  40. sb.AppendLine("</CuePoint>")
  41. Loop
  42.  
  43. sr.Close()
  44. sr.Dispose()
  45.  
  46. Dim result As String = sb.ToString()
  47. Console.WriteLine(result)
  48. End Sub
  49. End Class

Results in:
  1. <CuePoint>
  2. <Time>52.000000</Time>
  3. <Type>event</Type>
  4. <Name>Marker 400.000000</Name>
  5. <Parameters>
  6. <Parameter>
  7. <Name />
  8. <Value> 1.3107711</Value>
  9. </Parameter>
  10. </Parameters>
  11. </CuePoint>
  12. <CuePoint>
  13. <Time>51.000000</Time>
  14. <Type>event</Type>
  15. <Name>Marker 401.000000</Name>
  16. <Parameters>
  17. <Parameter>
  18. <Name />
  19. <Value> 2.3107711</Value>
  20. </Parameter>
  21. </Parameters>
  22. </CuePoint>
  23. <CuePoint>
  24. <Time>55.000000</Time>
  25. <Type>event</Type>
  26. <Name>Marker 402.000000</Name>
  27. <Parameters>
  28. <Parameter>
  29. <Name />
  30. <Value> 3.3107711</Value>
  31. </Parameter>
  32. </Parameters>
  33. </CuePoint>
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 10
Reputation: ktkevin1222 is an unknown quantity at this point 
Solved Threads: 0
ktkevin1222 ktkevin1222 is offline Offline
Newbie Poster

Re: Need Help TXT to XML Converter

 
0
  #7
Sep 16th, 2009
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).
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,258
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 581
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Need Help TXT to XML Converter

 
0
  #8
Sep 17th, 2009
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?
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 10
Reputation: ktkevin1222 is an unknown quantity at this point 
Solved Threads: 0
ktkevin1222 ktkevin1222 is offline Offline
Newbie Poster

Re: Need Help TXT to XML Converter

 
0
  #9
Sep 17th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,258
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 581
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Need Help TXT to XML Converter

 
0
  #10
Sep 18th, 2009
Just modify the code. I had it create the text file since I didn't have your file on my machine.

  1. Imports System.IO
  2. Imports System.Text
  3.  
  4. Public Class frmReadFile
  5.  
  6. Private Shared Sub CreateSampleFile(ByVal outFile As String)
  7. Dim sb As New StringBuilder()
  8. sb.AppendLine("52.000000, 400.000000, 1.3107711, 0")
  9. sb.AppendLine("51.000000, 401.000000, 2.3107711, 0")
  10. sb.AppendLine("55.000000, 402.000000, 3.3107711, 0")
  11. Dim buffer() As Byte = ASCIIEncoding.UTF8.GetBytes(sb.ToString())
  12. If (File.Exists(outFile)) Then
  13. File.Delete(outFile)
  14. End If
  15.  
  16. Dim fs As New FileStream(outFile, FileMode.Create)
  17. fs.Write(buffer, 0, buffer.Length)
  18. fs.Close()
  19. fs.Dispose()
  20. End Sub
  21.  
  22. Private Shared Sub TxtToXML(ByVal inFile As String, ByVal outFile As String)
  23. Dim sr As New StreamReader(inFile)
  24.  
  25. Dim sb As New StringBuilder()
  26.  
  27. Do While sr.Peek() >= 0
  28. Dim line As String = sr.ReadLine()
  29. Dim fields() As String = line.Split(New [Char]() {","c}, System.StringSplitOptions.None)
  30.  
  31. sb.AppendLine("<CuePoint>")
  32. sb.AppendLine(" <Time>" & fields(0).Trim() & "</Time>")
  33. sb.AppendLine(" <Type>event</Type>")
  34. sb.AppendLine(" <Name>Marker " & fields(1).Trim() & "</Name>")
  35. sb.AppendLine(" <Parameters>")
  36. sb.AppendLine(" <Parameter>")
  37. sb.AppendLine(" <Name />")
  38. sb.AppendLine(" <Value>" & fields(2).Trim() & "</Value>")
  39. sb.AppendLine(" </Parameter>")
  40. sb.AppendLine(" </Parameters>")
  41. sb.AppendLine("</CuePoint>")
  42. Loop
  43.  
  44. sr.Close()
  45. sr.Dispose()
  46.  
  47. Dim result As String = sb.ToString()
  48. If (File.Exists(outFile)) Then
  49. File.Delete(outFile)
  50. End If
  51. File.WriteAllText(outFile, sb.ToString())
  52. End Sub
  53.  
  54. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  55. 'This is since I don't have your file. You dont run this line since you already have the file.
  56. CreateSampleFile("C:\testdata.txt")
  57. 'This is the real work
  58. TxtToXML("C:\testdata.txt", "C:\testdata.xml")
  59. End Sub
  60. End Class
Last edited by sknake; Sep 18th, 2009 at 2:28 am.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Reply

Tags
text, txttoxmlconverter, vb2008, xml

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the VB.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC