Results 1 to 9 of 9

Thread: How can I retrieve data from a specific place in text?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    28

    Question redacted

    redacted
    Last edited by Reboh79; Jun 13th, 2015 at 06:11 AM. Reason: Redacting content

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: How can I retrieve data from a specific place in text?

    try this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim firstParts() As String
    4.  
    5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    6.         IO.File.WriteAllLines("filename", New String() {String.Join("", New String() {firstParts(0), "=", TextBox1.Text}), String.Join("", New String() {firstParts(1), "=", TextBox2.Text})})
    7.     End Sub
    8.  
    9.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    10.         Dim lines() As String = IO.File.ReadAllLines("filename")
    11.         ReDim firstParts(lines.GetUpperBound(0))
    12.         Dim parts() As String = lines(0).Split("="c)
    13.         firstParts(0) = parts(0)
    14.         TextBox1.Text = parts(1)
    15.         parts = lines(1).Split("="c)
    16.         firstParts(1) = parts(0)
    17.         TextBox2.Text = parts(1)
    18.     End Sub
    19.  
    20. End Class

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: How can I retrieve data from a specific place in text?

    If you have any control over the format of this file, I would suggest that you consider something like XML. After all, XML structures a text file with nodes and data associated with those nodes. Your description suggests that you don't want to be making random changes to the file, but to actually only change the values associated with certain tags. That's much more aligned with XML than pure text files.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    28

    redacted

    redacted
    Last edited by Reboh79; Jun 13th, 2015 at 06:10 AM. Reason: Redacting content

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: How can I retrieve data from a specific place in text?

    vb Code:
    1. Public Class Form1
    2.  
    3.     'array to store first parts of lines
    4.     Dim firstParts() As String
    5.  
    6.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    7.         'save changes. WriteAllLines saves a string array, so i created a string array, + rejoined the lines
    8.         IO.File.WriteAllLines("filename", New String() {String.Join("", New String() {firstParts(0), "=", TextBox1.Text}), String.Join("", New String() {firstParts(1), "=", TextBox2.Text})})
    9.     End Sub
    10.  
    11.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    12.         'read the file into an array of strings, each element being 1 line of the file
    13.         Dim lines() As String = IO.File.ReadAllLines("filename")
    14.         'dimension the firstParts array
    15.         ReDim firstParts(lines.GetUpperBound(0))
    16.         'split the first line into 2 parts
    17.         Dim parts() As String = lines(0).Split("="c)
    18.         'save the first part to the array
    19.         firstParts(0) = parts(0)
    20.         'put the second part in TextBox1
    21.         TextBox1.Text = parts(1)
    22.         'split the second line into 2 parts
    23.         parts = lines(1).Split("="c)
    24.         'save the first part to the array
    25.         firstParts(1) = parts(0)
    26.         'put the second part in TextBox2
    27.         TextBox2.Text = parts(1)
    28.     End Sub
    29.  
    30. End Class

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: How can I retrieve data from a specific place in text?

    here's an xml example:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         Dim xml As XDocument = _
    5.         <?xml version="1.0" encoding="utf-8"?>
    6.         <root>
    7.         </root>
    8.  
    9.         xml...<root>(0).Add( _
    10.         <people>
    11.             <name><%= TextBox1.Text %></name>
    12.             <age><%= TextBox2.Text %></age>
    13.         </people>)
    14.  
    15.         xml.Save("test.xml")
    16.  
    17.     End Sub
    18.  
    19.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    20.  
    21.         Dim xml As XDocument = Nothing
    22.  
    23.         If IO.File.Exists("test.xml") Then
    24.             xml = XDocument.Load("test.xml")
    25.  
    26.             Dim info = (From node In xml...<people> _
    27.                        Select New With { _
    28.                        .name = node.<name>.Value, _
    29.                        .age = node.<age>.Value}).ToArray
    30.  
    31.             TextBox1.Text = info(0).name
    32.             TextBox2.Text = info(0).age
    33.         End If
    34.     End Sub
    35.  
    36. End Class

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    28

    redacted

    redacted
    Last edited by Reboh79; Jun 13th, 2015 at 06:10 AM. Reason: Redacting content

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: How can I retrieve data from a specific place in text?

    it creates an xml file like this when you click the button.

    vb Code:
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <root>
    3.     <people>
    4.         <name>Jim</name>
    5.         <age>51</age>
    6.     </people>
    7. </root>

    in the form_load event, it checks if the xml file exists, + reads it.

    LINQ tutorial:

    http://searchwindevelopment.techtarg...de-LINQ-to-XML

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: How can I retrieve data from a specific place in text?

    One thing I would add is that XML is particularly easy to work with if you are dealing with a datatable, since you can serialize the table to XML with a single line, and deserialize back from XML just as easily. Considering your sample data, I would expect that you probably have more than one person (or else there are even better alternatives than XML), so you are probably storing the information somewhere. If you are using a datatable, then XML is definitely going to be better.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width