| | |
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:
Solved Threads: 0
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.
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.
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.
•
•
Join Date: Sep 2009
Posts: 10
Reputation:
Solved Threads: 0
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.
I still don't understand your formatting but you can format it however you want:
Results in:
VB.NET Syntax (Toggle Plain Text)
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:
text Syntax (Toggle Plain Text)
<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>
•
•
Join Date: Sep 2009
Posts: 10
Reputation:
Solved Threads: 0
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).
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?
Just modify the code. I had it create the text file since I didn't have your file on my machine.
VB.NET Syntax (Toggle Plain Text)
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
Last edited by sknake; Sep 18th, 2009 at 2:28 am.
![]() |
Similar Threads
- Looking for API (java) to convert .doc into .pdf (Java)
- database connection (Java)
- wxWidgets - RichTextCtrl - Is ther one? (Python)
- Help with automatic update problem and more (Viruses, Spyware and other Nasties)
- Uploading zip files (DaniWeb Community Feedback)
- Text file to XML (XML, XSLT and XPATH)
- Great Freewares (Windows Software)
- Read XML File in C/C++ (C++)
- Store Bluetooth remote address to a text file (C++)
Other Threads in the VB.NET Forum
- Previous Thread: Help with the FormClosing() Event
- Next Thread: .Net Framework
| Thread Tools | Search this Thread |
actionscript3 ads adsense ajax alignment api array asp.net assembly based black blogger blogging buy c# c++ code compression console createrange() delete design development dictionary edit editor error events external file flash flipbook game gdata getselection google html import javascript kernel keyboard label link linspire linus linux microsoft news node nodes openoffice optimisation parse perl php port portfolio printer programming python revenue review richtextbox rpg rss serialization soap speech split standards swappingxmlfromflash swappingxmlnodes swf synchronous tanenbaum tannenbaum technology text textbox torvalds transform txttoxmlconverter unicode unit vb2008 w3c web website win32 xml xmlnotloading xmlonserver xsl xslt







