I am currently using CKEditor for my project to read and display the content of a html file.
However, instead of getting the content of the file, all I get is a string: < html > display in the editor.
But if I write the content directly to the page using response.write, then all the content of the file is displayed correctly.
this is the code snippet I used to read the file:
strPathToConvert = Server.MapPath("~/convert/");
object filetosave = strPathToConvert + "paper.htm";
StreamReader reader = new StreamReader(filetosave.ToString());
string content = "";
while ((content = reader.ReadLine()) != null)
{
if ((content == "") || (content == " "))
{ continue; }
CKEditor1.Text = content;
//Response.Write(content);
}
Can anybody help me to solve this problem?
Many Thanks.
You are in a while loop and you are overwriting the contents of CKEditor every time since you use = instead of +=. Your loops should be:
StreamReader reader = new StreamReader(filetosave.ToString());
string content = "";
while ((content = reader.ReadLine()) != null)
{
if ((content == "") || (content == " "))
{ continue; }
CKEditor1.Text += content;
//Response.Write(content);
}
a better way would probably be to use
string content;
string line;
using (StreamReader reader = new StreamReader(filetosave.ToString())
{
while ((line= reader.ReadLine()) != null)
{
content += line;
}
}
CKEditor1.Text = content;
Related
I have a few trouble with my project.
My string is
11713,Julia's_Candy,Julia's Candy
1713,Julia's_Head,Julia's Head
Now I am using my code like this string line;
using (StreamReader file = new StreamReader(#"db.txt"))
{
while ((line = file.ReadLine()) != null)
{
if (line.Contains(("1713"+",")))
{
label1.Text = line.Split(',')[2];
}
}
}
I want Julia's Head to be print out but it have the same number that including in 11713,Julia's_Candy,Julia's Candy.And its print Julia's Candy
So I want to ask that how can I fix this code for more accurate.
Thank you
If you split first, you can use the first column as an Id column and search for exact match:
string line;
using (StreamReader file = new StreamReader(#"db.txt"))
{
while ((line = file.ReadLine()) != null)
{
var values = line.Split(",");
if (values[0] == "1713")
{
label1.Text = values[2];
}
}
}
I have an application which read from a file and shows the contents of the file in popups. the text file contain data in such a way;
D:\\abc\\xyz.txt|xyz.txt
D:\\MP3\\boom.mp3|boom.mp3
Now the application reads a line from the text file and shows the line content in another form, and these popups are displayed at once, what i want to do is, if there are multiple lines then each popup is shown after a pause of 10sec, but Thread.Sleep(10000)is not working for me. the code to read is given below.
void FileDetected()
{
int counter = 0,c=1;
string line;
System.IO.StreamReader file = new System.IO.StreamReader(FILEPATH);
while ((line = file.ReadLine()) != null)
{
string[] words = line.Split('|');
c = 1;
path = ""; fileName = "";
foreach (string word in words)
{
if (c == 1)
path = word.Replace(#"\\", #"\");
if (c==2)
fileName = word;
c++;
}
counter++;
if (path != "" && fileName != "")
{
Console.WriteLine("Path :" + path);
Console.WriteLine("FileName: " + fileName);
popup dlg = new popup(path, fileName);
dlg.Show();
//Thread.Sleep(10000);
}
}
kindly guide me.
If you are using .NET 4.5 (or 4.0 with Visual Studio 2012 or newer by downloading Microsoft.Bcl.Async via NuGet) it is very easy to do with async/await.
//You will likely need to modify the calling code to use async/await too.
async Task FileDetected()
{
int counter = 0,c=1;
string line;
System.IO.StreamReader file = new System.IO.StreamReader(FILEPATH);
while ((line = file.ReadLine()) != null)
{
string[] words = line.Split('|');
c = 1;
path = ""; fileName = "";
foreach (string word in words)
{
if (c == 1)
path = word.Replace(#"\\", #"\");
if (c==2)
fileName = word;
c++;
}
counter++;
if (path != "" && fileName != "")
{
Console.WriteLine("Path :" + path);
Console.WriteLine("FileName: " + fileName);
popup dlg = new popup(path, fileName);
dlg.Show();
//Pauses the loop for 10 sec but does not lock up the UI.
await Task.Delay(10000);
}
}
I have a html document that after being parsed contains only formatted text.I was wondering if it is possible to get its text like I would do if I was mouse-selecting it + copy + paste in new Text Document?
I know that this is possible in Microsoft.Office.Interop where I have .ActiveSelection property that selects the content of the open Word.
I need to find a way to load the html somehowe(maybe in a browser object) and then copy all of its content and assign it to a string.
var doc = new HtmlAgilityPack.HtmlDocument();
var documetText = File.ReadAllText(myhtmlfile.html, Encoding.GetEncoding(1251));
documetText = this.PerformSomeChangesOverDocument(documetText);
doc.LoadHtml(documetText);
var stringWriter = new StringWriter();
AgilityPackEntities.AgilityPack.ConvertTo(doc.DocumentNode, stringWriter);
stringWriter.Flush();
var titleNode = doc.DocumentNode.SelectNodes("//title");
if (titleNode != null)
{
var titleToBeRemoved = titleNode[0].InnerText;
document.DocumentContent = stringWriter.ToString().Replace(titleToBeRemoved, string.Empty);
}
else
{
document.DocumentContent = stringWriter.ToString();
}
and then I return the document object.The problem is that the string is not always formatted as I want it to be
You should be able to just use StreamReader and as you read each line just write it out using StreamWriter
Something like this will readuntil the end of your file and save it to a new one. If you need to do extra logic in the file I have a comment inserted to let you know where to do all that.
private void button4_Click(object sender, EventArgs e)
{
System.IO.StreamWriter writer = new System.IO.StreamWriter("C:\\XXX\\XXX\\XXX\\test2.html");
String line;
using (System.IO.StreamReader reader = new System.IO.StreamReader("C:\\XXX\\XXX\\XXX\\test.html"))
{
//Do until the end
while ((line = reader.ReadLine()) != null) {
//You can insert extra logic here if you need to omit lines or change them
writer.WriteLine(line);
}
//All done, close the reader
reader.Close();
}
//Flush and close the writer
writer.Flush();
writer.Close();
}
You can also save it to a string then just do whatever you want to with it. You can use new lines to keep the same format.
EDIT The below will tke into account your tags
private void button4_Click(object sender, EventArgs e)
{
String line;
String filetext = null;
int count = 0;
using (System.IO.StreamReader reader = new System.IO.StreamReader("C:\\XXXX\\XXXX\\XXXX\\test.html"))
{
while ((line = reader.ReadLine()) != null) {
if (count == 0) {
//No newline since its start
if (line.StartsWith("<")) {
//skip this it is formatted stuff
}
else {
filetext = filetext + line;
}
}
else {
if (line.StartsWith("<"))
{
//skip this it is formatted stuff
}
else
{
filetext = filetext + "\n" + line;
}
}
count++;
}
Trace.WriteLine(filetext);
reader.Close();
}
}
I Am using sharp develop. I am making a Win App using C# . I want my program check a text file named test in drive c: and find the line which contains "=" and then write this line to other newly created text file in drive c: .
Try this one-liner:
File.WriteAllLines(destinationFileName,
File.ReadAllLines(sourceFileName)
.Where(x => x.Contains("=")));
Here's another simple way using File.ReadLines, Linq's Where and File.AppendAllLines
var path1 = #"C:\test.txt";
var path2 = #"C:\test_out.txt";
var equalLines = File.ReadLines(path1)
.Where(l => l.Contains("="));
File.AppendAllLines(path2, equalLines.Take(1));
using(StreamWriter sw = new StreamWriter(#"C:\destinationFile.txt"))
{
StreamReader sr = new StreamReader(#"C:\sourceFile.txt");
string line = String.Empty;
while ((line = sr.ReadLine()) != null)
{
if (line.Contains("=")) { sw.WriteLine(line)); }
}
sr.Close();
}
Have you tried something?
Here are two ways to read a file:
Use static methods available in File class. ReadAllLines to be specific. This is good enough if you are dealing with small files. Next, once you have the array, just find the item with "=" using LINQ or by any other iteration method. Once you got the line, again use File class to create and write data to the file.
If you are dealing with large files, use Stream. Rest remains fairly same.
if (File.Exists(txtBaseAddress.Text))
{
StreamReader sr = new StreamReader(txtBaseAddress.Text);
string line;
string fileText = "";
while ((line = sr.ReadLine()) != null)
{
if (line.Contains("="))
{
fileText += line;
}
}
sr.Close();
if (fileText != "")
{
try
{
StreamWriter sw = new StreamWriter(txtDestAddress.Text);
sw.Write(fileText);
sw.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
a bit edited Furqan's answer
using (StreamReader sr = new StreamReader(#"C:\Users\Username\Documents\a.txt"))
using (StreamWriter sw = new StreamWriter(#"C:\Users\Username\Documents\b.txt"))
{
int counter = 0;
string line = String.Empty;
while ((line = sr.ReadLine()) != null)
{
if (line.Contains("="))
{
sw.WriteLine(line);
if (++counter == 4)
{
sw.WriteLine();
counter = 0;
}
}
}
}
I am trying to read some text from a txt file with following code:
using (StreamReader sr =
File.OpenText(System.IO.Path.GetFullPath("OrderEmailBody.txt")))
{
String input;
while ((input = sr.ReadLine()) != null)
{
emailBody += input;
}
}
The txt file has some blank lines and line breaks but this code is ignoring all line breaks and blank lines in txt file. Please suggest how to fix it?
It doesn't ignore them, you just don't add them to your mail body.
emailBody += input + Environment.NewLine;
using (StreamReader sr =
File.OpenText(System.IO.Path.GetFullPath("OrderEmailBody.txt")))
{
String input;
while ((input = sr.ReadLine()) != null)
{
emailBody += input;
email += Environment.NewLine;
}
}