?
Solved

vb.net line.replace

Posted on 2011-10-28
4
Medium Priority
?
411 Views
Last Modified: 2012-05-12
Hi,

I am in need of some assistance. I am trying to read a text file with a streamreader and am able to read to the line I want to replace that says "Test"

I have proven this to myself by throwing a msg box with the line I am looking for and am happy with this... I am now trying to replace this with a new string "new_value"

however when trying to write this new value it is not saving the change.

Please see code below. Any help you can provide will be greately received.


Dim rdr As StreamReader = New StreamReader("c:\test.xml")
        

        While Not rdr.EndOfStream
            Dim sfind As String = "Test"
            Dim sreplace As String = ""
            Dim line As String = rdr.ReadLine()
            If line.Contains(sfind) Then
                MsgBox(line)
                line = line.Replace(sfind, sreplace)
            End If
        End While
        rdr.Close()

Open in new window

0
Comment
Question by:SimonPrice33
  • 2
4 Comments
 
LVL 1

Expert Comment

by:Itpotter
ID: 37043662
Why not use XDocument or linq to xml for changing xml?
0
 
LVL 5

Accepted Solution

by:
mcs0506 earned 2000 total points
ID: 37043840
Hi,
Here is the sample method for replace and save changes to the text file.

Public Sub ReplaceFileData()
      Dim rdr As New StreamReader("c:\test.xml")
      Dim FileData As New List(Of String)()
      Dim sfind As String = "test"
      Dim sreplace As String = ""
      Dim line As String = rdr.ReadToEnd()
      line = line.Replace(sfind, sreplace)
      rdr.Close()
      rdr.Dispose()
      Dim wrt As New StreamWriter("c:\test.xml")
      wrt.Write(line.ToCharArray(), 0, line.ToCharArray().Length)
      wrt.Close()
      wrt.Dispose()
End Sub



Regards,

Dani
0
 

Author Comment

by:SimonPrice33
ID: 37044217
One of my proposed methods is to use the following method of writing my xml.


        Dim xdoc As New Xml.XmlDocument()

        Dim samples As Xml.XmlNode = xdoc.AppendChild(xdoc.CreateElement("Samples"))

        samples.AppendChild(xdoc.CreateElement("Stub")).InnerText = stubTxt.Text
        samples.AppendChild(xdoc.CreateElement("Date")).InnerText = dateLbl.Text
        samples.AppendChild(xdoc.CreateElement("Time")).InnerText = TimeLbl.Text
        samples.AppendChild(xdoc.CreateElement("Weight")).InnerText = cleanTxt.Text
        samples.AppendChild(xdoc.CreateElement("Tops")).InnerText = topsTxt.Text
       

        xdoc.Save("C:\test.xml")


my issue is that I have to work in .Net 2.0 and I have a very limited knowledge of writing XML. The XML I have written in the past was an LINQ document.

What I need from the code above is to write the record, then append new records to it...   As I am being pushed by my factory for the output I am looking to get a very quick and dirty resolution out there by using


        Dim rdr As StreamReader = New StreamReader("c:\test.xml")
       

        While Not rdr.EndOfStream
            Dim sfind As String = "Test"
            Dim sreplace As String = ""
            Dim line As String = rdr.ReadLine()
            If line.Contains(sfind) Then
                MsgBox(line)
                line = line.Replace(sfind, sreplace)
            End If
        End While
        rdr.Close()

        Dim sw As StreamWriter = File.AppendText("c:\test.xml")

        sw.WriteLine("<sample>")
        sw.WriteLine("<stub>" & stubTxt.Text & "</stub>")
        sw.WriteLine("<cleanWeight>" & cleanTxt.Text & "</cleanWeight>")
        sw.WriteLine("<topsWeight>" & topsTxt.Text & "</topsWeight>")
        sw.WriteLine("<Date>" & dateLbl.Text & "</Date>")
        sw.WriteLine("<Time>" & TimeLbl.Text & "</Time>")
        sw.WriteLine("</sample>")
        sw.Close()

this appends the results on after another, but outside of the root nods <samples> </samples>

so what I was looking to do here was replace the closing root node with the new record, and then re-writing the </samples> with the latest record.

If you are able to help me with that I would be extremely grateful.
0
 

Author Closing Comment

by:SimonPrice33
ID: 37044240
Worked Perfectly thank you :)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question