I have a website (php) with two lines.
I try to read the content with following code:
var url = "WEBSITE.php";
var client = new WebClient();
using (var stream = client.OpenRead(url))
using (var reader = new StreamReader(stream))
{
string line;
while ((line = reader.ReadLine()) != null)
{
//Do whatever
}
}
Unfortunately the string "line" contains not one line of the two lines. It contains the whole content with tags...is it possible to fetch line by line? or have i to delimit it by myself?
Thanks!
You could do that a little bit easier by using something like this:
string url = "WEBSITE.php";
string content = WebClient.DownloadString(url);
foreach (string line in content.Split(new string[] { "<br>", "</br>" }, StringSplitOptions.None))
{
// Do something with each "line" ;-)
}
Related
Hello i'm getting some strings from a Web API
Like This
mail-lf0-f100.google.com,209.85.215.100
mail-vk0-f100.google.com,209.85.213.100
mail-ua1-f100.google.com,209.85.222.100
mail-ed1-f100.google.com,209.85.208.100
mail-lf1-f100.google.com,209.85.167.100
mail-ej1-f100.google.com,209.85.218.100
mail-pj1-f100.google.com,209.85.216.100
mail-wm1-f100.google.com,209.85.128.100
mail-io1-f100.google.com,209.85.166.100
mail-wr1-f100.google.com,209.85.221.100
mail-vs1-f100.google.com,209.85.217.100
mail-ot1-f100.google.com,209.85.210.100
mail-qv1-f100.google.com,209.85.219.100
mail-yw1-f100.google.com,209.85.161.100
it give me some string records and i want to do this Operations.
I want to make it line by line like it show in original
I want to remove everything after comma in eachline
I want to set a prefix before each line for the example:
Example: Google.com to>> This is Dns: Google.com
and This is my code . What should i edit and what should i add?
string filePath = "D:\\google.txt";
WebClient wc = new WebClient();
string html = wc.DownloadString("https://api.hackertarget.com/hostsearch/?q=Google.com");
File.CreateText(filePath).Close();
string number = html.Split(',')[0].Trim();
File.WriteAllText(filePath, number);
MessageBox.Show("Finish");
you are actually close. Check the solution below
var filePath = #"C:\Users\Mirro\Documents\Visual Studio 2010\Projects\Assessment2\Assessment2\act\actors.txt";
WebClient client = new WebClient();
string html = client.DownloadString("https://api.hackertarget.com/hostsearch/?q=google.com");
string[] lines = html.Split(
new[] { "\r\n", "\r", "\n" },
StringSplitOptions.None
);
var res = lines.Select(x => (x.Split(',')[0]).Trim()).ToList();
//res.Dump();
System.IO.File.WriteAllLines(filePath, lines);
.Net Fiddle
Hi
Thanks to Derviş Kayımbaşıoğlu
Also i find my solution from another way
string path = #"D:\Google.txt";
var url = "https://api.hackertarget.com/hostsearch/?q=google.com";
var client = new WebClient();
//StreamWriter myhost = new StreamWriter(path);
using (var stream = client.OpenRead(url))
using (var reader = new StreamReader(stream))
{
string line;
string realString;
using (StreamWriter myhost = new StreamWriter(path))
{
while ((line = reader.ReadLine()) != null)
{
realString = "This is the dns: " + line.Split(',')[0].Trim();
myhost.WriteLine(realString);
}
}
}
MessageBox.Show("Finish");
I need some help with loading text file into a listView. Text file looks like this:
1,6 sec,5 sec,1 sec,17,
2,6 sec,4 sec,2 sec,33,
3,7 sec,5 sec,3 sec,44,
I have to load this into a listView control and every subitem should be separated by comma (or any other character). I tried something like this:
using (var sr = new StreamReader(file))
{
string fileLine = sr.ReadLine();
foreach (string piece in fileLine.Split(','))
{
listView1.Items.Add(piece);
}
sr.Close();
}
it would work just fine apart from only first line is loaded to the first column in listview. I cannot figure it out.
Thanks for your time!
KR!
You have to advance to the next line, you can use a while-loop:
using (var sr = new StreamReader(file))
{
string fileLine;
while ((fileLine = sr.ReadLine()) != null)
{
foreach (string piece in fileLine.Split(','))
{
listView1.Items.Add(piece);
}
}
}
Note that you don't need to close the stream manually, that is done by the using-statement.
Another way is using File.ReadLines or File.ReadAllLines which can help to simplify your code:
var allPieces = File.ReadLines(file).SelectMany(line => line.Split(','));
foreach(string piece in allPieces)
listView1.Items.Add(piece);
using (var sr = new StreamReader(file))
{
while(!sr.EndOfStream)
{
string fileLine = sr.ReadLine();
foreach (string piece in fileLine.Split(','))
{
listView1.Items.Add(piece);
}
sr.Close();
}
}
Ι guess you just have to add:
while (!sr.EndOfStream)
{
string fileLine = sr.ReadLine();
foreach (string piece in fileLine.Split(','))
{
listView1.Items.Add(piece);
}
}
sr.Close();// close put the end of while scope beacause you have a multiline text this code can't be read second line, and throw exceptions this code.
i want to remove stop words from my text file and i write the following code for this purpose
TextWriter tw = new StreamWriter("D:\\output.txt");
private void button1_Click(object sender, EventArgs e)
{
StreamReader reader = new StreamReader("D:\\input1.txt");
string line;
while ((line = reader.ReadLine()) != null)
{
string[] parts = line.Split(' ');
string[] stopWord = new string[] { "is", "are", "am","could","will" };
foreach (string word in stopWord)
{
line = line.Replace(word, "");
tw.Write("+"+line);
}
tw.Write("\r\n");
}
but it doesn't show the result in the output file and the output file remain empty.
A regular expression might be perfect for the job:
Regex replacer = new Regex("\b(?:is|are|am|could|will)\b");
using (TextWriter writer = new StreamWriter("C:\\output.txt"))
{
using (StreamReader reader = new StreamReader("C:\\input.txt"))
{
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
replacer.Replace(line, "");
writer.WriteLine(line);
}
}
writer.Flush();
}
This method will only replace the words with blanks and do nothing with the stopwords if they are part of another word.
Good luck with your quest.
The following works as expected for me. However, it's not a good approach because it will remove the stop words even when they are part of a larger word. Also, it doesn't clean up extra spaces between removed words.
string[] stopWord = new string[] { "is", "are", "am","could","will" };
TextWriter writer = new StreamWriter("C:\\output.txt");
StreamReader reader = new StreamReader("C:\\input.txt");
string line;
while ((line = reader.ReadLine()) != null)
{
foreach (string word in stopWord)
{
line = line.Replace(word, "");
}
writer.WriteLine(line);
}
reader.Close();
writer.Close();
Also, I recommend using using statements for when you create your streams in order to ensure the files are closed in a timely manner.
You should wrap your IO objects in using statements so that they are disposed properly.
using (TextWriter tw = new TextWrite("D:\\output.txt"))
{
using (StreamReader reader = new StreamReader("D:\\input1.txt"))
{
string line;
while ((line = reader.ReadLine()) != null)
{
string[] parts = line.Split(' ');
string[] stopWord = new string[] { "is", "are", "am","could","will" };
foreach (string word in stopWord)
{
line = line.Replace(word, "");
tw.Write("+"+line);
}
}
}
}
Try wrapping StreamWriter and StreamReader in using() {} clauses.
using (TextWriter tw = new StreamWriter(#"D:\output.txt")
{
...
}
You may also want to call tw.Flush() at the very end.
I am having a little trouble with the streamreader.
I am opening emails from the file dialog, and those emails are placed inside a listbox.
each letter in the emails, are on one line, as shown in the picture below.
I want the emails to be on one line, can some one help me, this is giving me a headache.
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog ofg = new OpenFileDialog();
ofg.Filter = "Text Files|*.txt";
if (ofg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
var fileName = ofg.FileName;
StreamReader sr = new StreamReader(File.OpenRead(fileName));
var line = sr.ReadToEnd();
foreach (var l in line)
listBox1.Items.Add(l.ToString());
sr.Dispose();
}
}
var lines = File.ReadAllLines( fileName );
foreach (var l in lines )
{
listBox1.Items.Add( l );
}
assuming that you have
email1#email1.com
email2#email2.com
in your file (this is what I understood from your description).
use this:
string line;
while((line = reader.ReadLine()) != null)
listBox1.Items.Add(line);
Use it as follow:
using (StreamReader sr = new StreamReader(File.OpenRead(fileName)))
{
string line;
while ((line = sr.ReadLine()) != null)
{
listBox1.Items.Add(line.ToString());
}
}
This reads all the lines in the file and adds it to the listbox line by line.
string containl chars, so foreach (var l ...) iterates through chars in line.
You should replace your foreach with
foreach( var email in line.Split(' '))
In case your emails separated with spaces.
Another approach would be File.ReadAllLines, in case emails in your file is on separate lines...
using (StreamReader sr = new StreamReader(File.OpenRead(fileName)))
{
while (sr.Peek() >= 0)
{
listBox1.Items.Add(sr.ReadLine());
}
}
Reference: http://msdn.microsoft.com/en-us/library/system.io.streamreader.readline
How would it be possible to read a text file with several lines, and then to put each line in the text file on a separate row in a ListBox?
The code I have so far:
richTextBox5.Text = File.ReadAllText("ignore.txt");
String text = File.ReadAllText("ignore.txt");
var result = Regex.Split(text, "\r\n|\r|\n");
foreach(string s in result)
{
lstBox.Items.Add(s);
}
string[] lines = System.IO.File.ReadAllLines(#"ignore.txt");
foreach (string line in lines)
{
listBox.Items.Add(line);
}
Write a helper method that return the collection of lines
static IEnumerable<string> ReadFromFile(string file)
{// check if file exist, null or empty string
string line;
using(var reader = File.OpenText(file))
{
while((line = reader.ReadLine()) != null)
{
yield return line;
}
}
}
use it
var lines = ReadFromFile(myfile);
myListBox.ItemsSource = lines.ToList(); // or change it to ObservableCollection. also you can add to the end line by line with myListBox.Items.Add()
You should use a streamreader to read the file one line at a time.
using (StreamReader sr = new StreamReader("ignore.txt"))
{
string line;
while ((line = sr.ReadLine()) != null)
listBox1.Items.Add(line);
}
StreamReader info -> http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx
ListBox info -> http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.aspx