reading a specific data in .txt file to put in specific tag in xml in vb.net..pls help..

sample: BL% PHP 103025 and so on...
NL%
..
..

BL% and NL% are indicators where data should be placed in a specific segment in xml
PHP must be placed inside <currency></currency> tag..
103025 must be placed inside <amount></amount> tag..

Dani AI

Generated

Good start — detecting the BL%/NL% markers and tokenizing is the right idea. A few robustness improvements and a short example that actually builds XML will make this ready for real files:

  • Use StartsWith instead of Substring(0,3) so blank/short lines can't throw exceptions, and normalize with Trim and StringComparison.OrdinalIgnoreCase.
  • Split on whitespace with StringSplitOptions.RemoveEmptyEntries so extra spaces don't create empty tokens.
  • Validate/parse amounts with Decimal.TryParse (use InvariantCulture if amounts are machine-formatted). If parsing fails, keep the raw string but log it.
  • Let XElement / XDocument write XML (they escape special characters automatically) instead of building tags by string concatenation. If you must insert into an existing XML document, Load it and add elements to the right parent rather than recreating the whole file.

Example (concise VB.NET using LINQ to XML):

Imports System.Xml.Linq
Imports System.Globalization
Imports System.IO

Sub ConvertFileToXml(inPath As String, outPath As String)
    Dim doc As New XDocument(New XElement("root"))

    For Each rawLine As String In File.ReadLines(inPath)
        If String.IsNullOrWhiteSpace(rawLine) Then Continue For
        Dim line = rawLine.Trim()

        If line.StartsWith("BL%", StringComparison.OrdinalIgnoreCase) Then
            Dim payload = line.Substring(3).Trim()
            Dim tokens() As String = payload.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries)
            Dim currency = If(tokens.Length > 0, tokens(0).ToUpperInvariant(), String.Empty)
            Dim amountStr = If(tokens.Length > 1, tokens(1), String.Empty)

            Dim amountDec As Decimal
            If Decimal.TryParse(amountStr, NumberStyles.Number, CultureInfo.InvariantCulture, amountDec) Then
                doc.Root.Add(New XElement("payment",
                    New XElement("currency", currency),
                    New XElement("amount", amountDec.ToString(CultureInfo.InvariantCulture))))
            Else
                doc.Root.Add(New XElement("payment",
                    New XElement("currency", currency),
                    New XElement("amount", amountStr)))
            End If

        ElseIf line.StartsWith("NL%", StringComparison.OrdinalIgnoreCase) Then
            Dim payload = line.Substring(3).Trim()
            doc.Root.Add(New XElement("nl", payload))
        End If
    Next

    doc.Save(outPath)
End Sub

Troubleshooting notes: explicitly set encoding when reading if the file has a BOM or non-UTF8 bytes; wrap IO in Try/Catch to report malformed lines; and if multiple BL% entries must map to different XML positions, load the target XML and insert elements at the correct node rather than always adding to the document root. This addresses common pitfalls that can surface in real-world text files.

This code may help:

Private Sub Form1_Load(ByVal sender As System.Object, _
                           ByVal e As System.EventArgs) _
                           Handles MyBase.Load
        Dim filename As String = "afile.txt" ' Your file name/path
        Dim filereader As New System.IO.StreamReader(filename) ' Open the file for reading
        Dim readline As String
        Dim data As String
        Dim dataparts(1) As String

        While Not filereader.EndOfStream
            readline = filereader.ReadLine ' Reads a line from the text file

            Select Case readline.Substring(0, 3) ' Reads the first 3 Characters of the line
                Case "BL%"
                    data = readline.Substring(4) ' Reads the data in front of 'BL%'
                    dataparts = data.Split(" ") ' Breaks the data into two and store them in the dataparts array
                    ' Add your codes for writing the data to the XML file in the 'BL%' segment between the currency and amount tag here

                Case "NL%"
                    data = readline.Substring(4)
                    dataparts = data.Split(" ")
                    ' Add your codes for writing the data to the XML file in the 'NL%' segment here
            End Select
        End While

    End Sub

I am assuming that you already know how to add the data to XML file(if you don't, you may learn it ) and that the data from text file you are reading from is stored in this way:
BL% PHP 103025
NL% Tag1 Tag2

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.