I wrote a program to crawl website to get data and output to a excel sheet. The program is written in C# using Microsoft Visual Studio 2010.
For most of the time, I have no problem getting content from the website, parse it, and store data in excel.
However, once a will I'll run into issue, saying that there are illegal characters (such as ▶) that prevents outputting to excel file, which crashes the program.
I also went onto the website manually and found other illegal characters such as Ú.
I tried to do a .Replace() but the code can't seem to find those characters.
string htmlContent = getResponse(url); //get full html from given url
string newHtml = htmlContent.Replace("▶", "?").Replace("Ú", "?");
So my question is, is there a way to strip out all characters of those types from a html string? (the html of the web page) Below is the error message I got.
I tried Anthony and woz's solution and that didn't work...
See System.Text.Encoding.Convert
Example usage:
var htmlText = // get the text you're trying to convert.
var convertedText = System.Text.Encoding.ASCII.GetString(
System.Text.Encoding.Convert(
System.Text.Encoding.Unicode,
System.Text.Encoding.ASCII,
System.Text.Encoding.Unicode.GetBytes(htmlText)));
I tested this with the string ▶Hello World and it gave me ?Hello World.
You could try stripping all non-ASCII characters.
string htmlContent = getResponse(url);
string newHtml = Regex.Replace(htmlContent, #"[^\u0000-\u007F]", "?");
thank you for the replies and thanks for the help.
After couple more hours of googling I have found the solution to my question. The problem was that I had to "sanitize" my html string.
http://seattlesoftware.wordpress.com/2008/09/11/hexadecimal-value-0-is-an-invalid-character/
Above is the helpful article I found, which also provides code example.
Related
I have a database table that containing file paths of excel files that I import using a C# script.
The script works fine unless the filepath contains spaces e.g. C:\Temp\My Excel File.xls and I get an Illegal characters in path error message. Unfortunately I am not able to change the file names at the source.
If I hard code the file path to be as below it works fine.
String Filepath = #"C:\Temp\My Excel File.xls";
How do I alter this so I can include a string variable that will store the filepath from the database e.g.
String Filepath = //Code to get FilePath from database
StringCorrectedFilePath = #+FilePath;
Thanks in advance of any help
Edit: Issue is caused by files that start with a number creating invalid escape sequence. e.g. C:\Temp\20160611 My Excel File.xls
Edit 2: SOLVED - Error was caused by carriage return characters appearing after the file extension. Please see my answer for the solution.
Whether you do this
String Filepath = #"C:\Temp\My Excel File.xls";
or this
String Filepath = "C:\\Temp\\My Excel File.xls";
the string stored in memory is just C:\Temp\My Excel File.xls, whatever the debugger may tell you. So when you read some string from somewhere (database, file, user input, ...) you don't need to "escape" backslashes. So just use that string.
Path.GetInvalidFileNameChars
FilePath = string.Concat(FilePath.Split(System.IO.Path.GetInvalidFileNameChars())).Trim();
Well you can replace blank space with %20 character and while retrieving replace back with blank space again like (you may as well choose to use regular expression for the same)
String Filepath = #"C:\Temp\My Excel File.xls";
Filepath = Filepath.Replace(" ", "%20");
While retrieving back
string mypath = pathyouhavegotfromDB.Replace("%20", " ");
I think you need to put quotation marks around the path with spaces.
string filepath = #"C:\Temp\My Excel File.xls";
filepath = $"\"{filepath}\"";
Thanks for everyone's help, I tried all of these and unfortunately they didn't work which led me to believe that the issue wasn't what I originally thought.
It turns out that the files causing the Illegal characters in path all had carriage return characters at the end of the file name, after the file extension.
To resolve this I used the following code and now it works perfectly
FilePath = FilePath.TrimEnd('\r', '\n');
Thanks everyone for your help.
Try this:
String StringCorrectedFilePath = #""+ Filepath;
Maybe someone can help me with the following problem.
I have a rich textbox control: RichTxtDescription.
I fill the text like:
String _description = “Some text with rft tags like \\ine , \b, \b01 etc.”;
RichTxtDescription.Rft = #"{\rtf1\ansi " + _Description + "\\line \\line Query: \\line .}";
In my winform application, the richtextbox control gets filled as it should be(text with new lines, bolds etc).
Now, I want to convert the richTxtDescription.Rft to html tags, to show it on a rtf field what only accepts html tags.
Any suggestions?
Thanks!
I used this RTF Converter yesterday :) and I am very happy about it.
Download that project
Compile the solution "RtfWinForms2010.sln".
In the bin folder (same level as the solution), you will find the DLL's that you can use. Their names start with "Itenso". The one you might be interested in will be "Itenso.Rtf.Converter.Html.dll" and the ones that this DLL depends on.
Add reference to your project and use the following snippet as starting point.
IRtfDocument rtfDocument = RtfInterpreterTool.BuildDoc( yourRtfVariable );
RtfHtmlConverter htmlConverter = new RtfHtmlConverter(rtfDocument);
string html = htmlConverter.Convert();
I have a WPF application that sends out a HTML-formatted email when a button is clicked. The entire email message is in HTML-format and it does work.
However, I was wondering if there was a way to read a html file and send it out rather than writing the whole message in the code behind...keeping all the HTML formatting in-tact.
I tried something like this:
string MessageTosend = File.ReadAllText("path to txt/html file");
But that just sent out an email that only has text (no styling, no html...just the plain text found in the file).
Then I thought, I may have to convert everything:
string MessageTosend = Convert.ToString(File.ReadAllText("path to txt/html file"));
But that does the same thing as before.
Is there a way to do achieve this? Or will I have to stick to having
string MessageTosend = #"<html> ... lots of html stuff ... </html>";
for every button that sends an email?
For notice: The contents of the .txt and .html file I attempted to read from was tested using the same contents of the above string (which, again, works as expected), and without the double quotes (example: width=""100"" and width="100")
Try adding an encoding to your file read:
string MessageTosend = File.ReadAllText("path to txt/html file", Encoding.UTF8);
Try reading a file simply containing < and compare it to the string "<". Repeat for any special characters until you find a mismatch. Then find the character number like this:
(int)MessageTosend[0] // < should be 60 (3C in UTF-8)
Find out what the offending characters are, and we may be able to help. If I read a file, I do not see this problem.
I want to read the html file.And for that I use System.IO.File.ReadAllText(path).It can read all the html file but there is one file which is not read through this function.
I have also used
using (StreamReader reader = File.OpenText(fileName)) {
text = reader.ReadToEnd(); But still there is same problem.
What is the reason can be there ? And for that what can be the solution ? Or any other way to read the file ?
I'll take a wild guess:
The file contains unicode sequences for extended chars and the diagnose is based on (mismatched) length.
if I debug the code in the it looks
like
"<\0h\0t\0m\0l\0>\0<\0h\0e\0a\0d\0>\0\r\0\n\0<\0M\0E\0T\0A\0
\0h\0t\0t\0p\0-\0e\0q\0u\0i\0v\0=\0\"\0C\0o\0n\0t\0e\0n
Which is a valid beginning of a HTML file except for the very first char. The file is probably damaged by missing a unicode marker at the start. This damage was probably caused when it was written and is not (easy) repairable now.
You could try setting the WebClient.Encoding to UTF8 (and try a few ASCII as well).
Does MsgBox shows anything? Any error? What does varText.Length show?
string varText = File.ReadAllText(varFile, Encoding.Default);
MessageBox.Show(varFile + " Text: " + varText + " Lenght: " + varText.Length);
Verify in MessageBox that the path to file is correct, verify that the access rights from inside your application are the same as if you would be reading the file with notepad.
Came across this on google recently. The correct way to do it is via WebClient...
WebClient client = new WebClient();
String guestMsg = client.DownloadString("C:\\temp\\TheBarGuestDetailsEmail.htm");
File.ReadAllText will mess up the html when it's doing a read, and characters like £ or ' will get messed up.
I have the following C# code to produce a small PHP file. The reason I am doing this is to update 400 plus sites automatically. The sites are in PHP on a Windows Environment so using C# for utility apps is the easiest for me.
fileContents.AppendFormat("<?php{0}",Environment.NewLine);
fileContents.AppendFormat("# FileName=\"clientsite.php\"{0}",Environment.NewLine);
fileContents.AppendFormat("# HTTP=\"true\"{0}",Environment.NewLine);
fileContents.AppendFormat("$clientname = \"{0}\";{1}", clientsiteName, Environment.NewLine);
fileContents.AppendFormat("$version = \"v6.2i\";{0}",Environment.NewLine);
fileContents.Append("?>");
The end result of this file causes a strange character to appear on the PHP page that includes this page. When I manually open the created PHP file - press backspace on the last line then enter it works. Is there something better than Environment.NewLine to use for this? Or is there another problem I am missing?
EDIT: The character looks like something I can't reproduce on the keyboard (squiggle line) by ends with ?
You could just try "\n", I believe Environment.NewLine is "\r\n".
But it could also be about how you write the StringBuilder (I assume fileContents is a StringBuilder) to the file. If you e.g. use WriteAllText, you could try using different encoding.