1

I'm getting a file content from my website:

    Dim client As WebClient = New WebClient()
    Dim text = client.DownloadString("http://myurl.com/raw.php")

The text is following:

Line
Another Line
Test1
Test2

Now, how can I run a loop through the text variable, to have a text from each line ?

I have tried this:

    Dim str As String() = text.Split(":::")
    For Each line As String In str
        MsgBox(line)
    Next

And I was finishing each line with the :::, but this method seems ugly and I hope there are better solutions?

5
  • Have you thought of returning an XML string rather than plain text? Commented Nov 22, 2012 at 16:55
  • @bendataclear Well, I am not against such solution. :) Commented Nov 22, 2012 at 16:56
  • text is not a Dim but a variable. Dim is the statement used to declare local variables and class members. Commented Nov 22, 2012 at 17:00
  • XML might be a good solution but it depends on exactly what data you're returning via the php file. Commented Nov 22, 2012 at 17:02
  • @bendataclear Just the string, containg the article news. Commented Nov 22, 2012 at 17:03

4 Answers 4

6
Dim lines As String() = text.Split(Environment.NewLine)
For Each line As String In lines
    MsgBox(line)
Next

This maybe doesn't work because the newline character in the original string is different from Environment.Newline. This can happen if the string comes from a Unix-based source.

An alternative approach could be:

Dim reader = new StringReader(text)
While True
    Dim line = reader.ReadLine()
    If line is Nothing
        Exit While
    Else
        MsgBox(line)
    End If
End While
Sign up to request clarification or add additional context in comments.

9 Comments

You may want to change text.Split(Environment.NewLine) to text.Split(CChar(Environment.NewLine)) the former gives me an error of course I have Option Strict On
I dont know why, but it doesn't really works for me since for the Hello<newline>World (<newline> is just a new line in the text, as the comments here doesn't allow to place new lines) it does return just one MsgBox containing Hello<newline>World instead of two MsgBoxe's - one with Hello and second World. Hope you understand it.
@Scott are you saying that you have an XML Style document that you are parsing, with <newline> being your delineator?
@MarkHall No, I said that <newline> is just a "sign" that new line is placed there, since I can not place new line here, in comments. I mean this: pastebin.com/raw.php?i=Q4v8bHrH
@MarkHall: Since Environment.NewLine returns the two-character sequence CRLF on Windows systems, CChar(Environment.NewLine) returns just CR, the first character. If the source of the data is from a unix system, it is likely separated by LFs.
|
2

An example of the vb.net side if useing XML:

Dim doc As New System.Xml.XmlDocument
doc.Load("http://myurl.com/raw.php")

Dim list = doc.GetElementsByTagName("article")

For Each item As System.Xml.XmlElement In list
    MsgBox(item.InnerText)
Next

Edit:

Example XML schema:

<?xml version='1.0'?>
<!-- File generated at 22/11/2012 17:22 -->
<articleList>
  <article>Hello</article>
  <article>World</article>
  <article>Fish</article>
</articleList>

Another advantage of switching to XML now will mean it will be easier to add more details in the future (like article header, date/time, auther etc).

4 Comments

Could you post the XML schema, that PHP should return? Something like... <xml><article>Hello</article><article>World</article></xml> ?
Also, how can I display (and get them inside of the loop) additional data such as date or article author as you said?
Yea, nevermind I have fixed all my questions/problems on my own :).
1

Using text.Split(CChar(Environment.NewLine)), as suggested in another answer, is not optimal: Environment.NewLine returns the two-character sequence CR LF on Windows systems. Thus, CChar(Environment.NewLine) returns just CR, the first character of the string. If the source of the data is from a unix system, it is likely separated by LFs.

If you are unsure about the exact line endings, you could use the following:

Dim lines As String() = text.Split(new String() {vbCrLf, vbCr, vbLf}, 
                                   StringSplitOptions.None)

That should cover all cases, since it splits on CR alone, on LF alone and on a combination of both.

Comments

-1

Sorry to bring this up years later... I just found a easier way to do it:

dim lin() as string
lin() = TextBox1.Text.Split(vbCrLf)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.