473,758 Members | 8,297 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to read html files AS IS. Encoding seems to change the characters.

My task is to read html files from disk and save them onto SQL Server
database field. I have created an nvarchar(max) field to hold them.
The problem is that some characters, particularly html entities, and
French/German special characters are lost and/or replaced by a
question mark.
This is really frustrating. I have tried using StreamReader with ALL
the encodings available and none work correctly. Each encoding handles
some characters and but loses others. I also tried reading into byte
array, but as soon as I converted the array to string the encoding
ruined the text.
Maybe the solution is not to convert to string? but then how will I
save it to the database?

Is there a way to get this html text AS IS - with no encoding and no
changes into the database?
I could do it in Delphi and many other pre .NET, there must be a way
in C# too - surely?!

Thanks a lot for your help,
zoro.

Mar 30 '07 #1
14 5770
Hello!
You wrote on 30 Mar 2007 10:43:07 -0700:

ZIs there a way to get this html text AS IS - with no encoding and no
Zchanges into the database?

Of course there's a way - use byte[] as a container, and use BLOB in the DB.
Don't use strings, as you will get all sorts of problems (as you already
do).

With best regards,
Eugene Mayevski
http://www.SecureBlackbox.com - the comprehensive component suite for
network security

Mar 30 '07 #2
On 30 Mar, 19:04, "Eugene Mayevski" <mayev...@eldos .comwrote:
Hello!
You wrote on 30 Mar 2007 10:43:07 -0700:

ZIs there a way to get this html text AS IS - with no encoding and no
Zchanges into the database?

Of course there's a way - use byte[] as a container, and use BLOB in the DB.Don't use strings, as you will get all sorts of problems (as you already
do).

With best regards,
Eugene Mayevskihttp://www.SecureBlack box.com- the comprehensive component suite for
network security
Thanks for that Eugene.
Unfortunatly, I was since told that the data has to be stored in text
format so that it may be cut and past into a text editor directly from
database and also for other applications to be able to retrieve it.
I also found that the problem is purely with C# reading the string -
the nvarchar field doesn't affect it.

Is it possible to read the file into a byte[] and INSERT the array
directly into a nvarchar field? if so how?

I am still intrigued about how to do it in a string though? how come
Delphi and VB6 can read a file into a string, without changing it's
characters? there must be a way to do it in C# !?

Thanks.
zoro.

Mar 30 '07 #3
Is it possible to read the file into a byte[] and INSERT the array
directly into a nvarchar field? if so how?

I am still intrigued about how to do it in a string though? how come
Delphi and VB6 can read a file into a string, without changing it's
characters? there must be a way to do it in C# !?
No, and notbody can do it, Delphi or VB.

nvarchar is Unicode text. HTML files are also text, but the encoding is
not always known. You can determine it if is properly tagged with
a meta http-equiv tag (for example <meta http-equiv="Content-Type"
content="text/html; charset=utf-8" />)

Thing is, you cannot get bytes and store them as nvarchar (o varchar),
because you will get crap. It might seem to work sometimes, but fail
spectacularly other times (depending on the content and encoding of
your html)
--
Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
Mar 31 '07 #4
Thing is, you cannot get bytes and store them as nvarchar (o varchar),
because you will get crap. It might seem to work sometimes, but fail
spectacularly other times (depending on the content and encoding of
your html)
Thanks for that Mihai. So do you agree with Eugene that the best way
forward for me is to read the file into byte[] and store in a
varbinary field?
If so, I will try to convince my management that it is the best way to
go forward, but I have a couple of questions about it:

What is the best (safest/fastest) method to read html file into byte[]
and store this array into a varbinary field?
How should the data be retrieved from the varbinary field, in order to
save it back as an html file - (must be identical to the original)?

Thank you,
zoro.

Mar 31 '07 #5
What is the best (safest/fastest) method to read html file into byte[]
and store this array into a varbinary field?
How should the data be retrieved from the varbinary field, in order to
save it back as an html file - (must be identical to the original)?
Sorry, but I am not familiar enough with the way .NET works with databases
to tell you exactly how to do this.

Thanks for that Mihai. So do you agree with Eugene that the best way
forward for me is to read the file into byte[] and store in a
varbinary field?
Yes. A binary field is the only way to 100% maintain a file unchanged.
But this is not going to be very useful for any kind of processing.
So, if the main goal is storing the htmls, then this is ok.
If you need searching, indexing, etc., then the db should know that is text,
and we get back to square 1.
--
Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email
Mar 31 '07 #6
Zoro wrote:
My task is to read html files from disk and save them onto SQL Server
database field. I have created an nvarchar(max) field to hold them.
The problem is that some characters, particularly html entities, and
French/German special characters are lost and/or replaced by a
question mark.
This is really frustrating. I have tried using StreamReader with ALL
the encodings available and none work correctly.
Have you really tried with ALL available encodings, or just the ones
that are predefined in the Encoding class? I counted to 140 supported
encodings in the documentation, of which 36 are supported on all
systems. You can create an Encoding object for any of those.

Examples:

Encoding windows1252 = Encoding.GetEnc oding(1252);
Encoding dosWesternEurop ean = Encoding.GetEnc oding(850);
Encoding iso8859_1 = Encoding.GetEnc oding(28591);

ISO-8859-1 is the default encoding for the MIME type text/html. That is
the content type usually used for web pages, so my first guess would be
that the files are encoded that way.
Each encoding handles
some characters and but loses others. I also tried reading into byte
array, but as soon as I converted the array to string the encoding
ruined the text.
Maybe the solution is not to convert to string? but then how will I
save it to the database?

Is there a way to get this html text AS IS - with no encoding and no
changes into the database?
As the file is not text at all, but only binary data, there is no way of
decoding the data into text without decoding it.
I could do it in Delphi and many other pre .NET, there must be a way
in C# too - surely?!
Of course there is. You just have to find out what encoding was used to
create the file, then you can decode it.

--
Göran Andersson
_____
http://www.guffa.com
Mar 31 '07 #7
Thanks for the help Goran.
Have you really tried with ALL available encodings, or just the ones
that are predefined in the Encoding class? I counted to 140 supported
encodings in the documentation, of which 36 are supported on all
systems. You can create an Encoding object for any of those.

Examples:

Encoding windows1252 = Encoding.GetEnc oding(1252);
Encoding dosWesternEurop ean = Encoding.GetEnc oding(850);
Encoding iso8859_1 = Encoding.GetEnc oding(28591);
You were right - I only tried the predefined - I didn't know about the
others. But I have now, and none seem to result in the same file.
I am running a very simple test:

I run the following code:
Encoding enc = Encoding.GetEnc oding(28591);
string html = File.ReadAllTex t("test.html" , enc);
File.WriteAllTe xt("test-1.html", html);
return;

I then compare test.html with test-1.html in ExamDiff text comparing
utility and I am getting many differences. All I want it to be able to
read text and write it back UNCHANGED.

Unfortunately, there is no encoding meta-data in the html but it has
words like "Ménière" in it and I lose those accented characters
between the files.

You are saying that the file is really a binary rather than a text
file. This is true, but my C# is not good eough to do what is required
as a result:
1. reading it as binary
2. storring it as binary in the database and most importantly
3. restoring it from database to UNCHARGED file on demand.

Can you help me with these tasks?

Thanks a lot,
zoro.

Apr 1 '07 #8
Zoro wrote:
Thanks for the help Goran.
>Have you really tried with ALL available encodings, or just the ones
that are predefined in the Encoding class? I counted to 140 supported
encodings in the documentation, of which 36 are supported on all
systems. You can create an Encoding object for any of those.

Examples:

Encoding windows1252 = Encoding.GetEnc oding(1252);
Encoding dosWesternEurop ean = Encoding.GetEnc oding(850);
Encoding iso8859_1 = Encoding.GetEnc oding(28591);

You were right - I only tried the predefined - I didn't know about the
others. But I have now, and none seem to result in the same file.
I am running a very simple test:

I run the following code:
Encoding enc = Encoding.GetEnc oding(28591);
string html = File.ReadAllTex t("test.html" , enc);
File.WriteAllTe xt("test-1.html", html);
return;

I then compare test.html with test-1.html in ExamDiff text comparing
utility and I am getting many differences. All I want it to be able to
read text and write it back UNCHANGED.
You are writing it back as UTF-8, as you are not specifying any encoding
in the WriteAllText method call. Can the ExamDiff utility compare files
with different encoding?
Unfortunately, there is no encoding meta-data in the html but it has
words like "Ménière" in it and I lose those accented characters
between the files.

You are saying that the file is really a binary rather than a text
file. This is true, but my C# is not good eough to do what is required
as a result:
1. reading it as binary
2. storring it as binary in the database and most importantly
3. restoring it from database to UNCHARGED file on demand.
Use File.ReadAllByt es to read the file into a byte array. You can save
that in an image field in the database. When you retrieve the data, you
get a byte array that you can save to a file using File.WriteAllBy tes.

--
Göran Andersson
_____
http://www.guffa.com
Apr 1 '07 #9
Thanks again Goran for your help.
You are writing it back as UTF-8, as you are not specifying any encoding
in the WriteAllText method call.
It looks like I may be able to do it with string after all.
It looks like the problem with my test was - like you suggested - that
i didn't specify the write encoding. When I do, as long as I use the
same encoding when reading and writing, it worked with all 3 codes you
have suggested (but not with any of the built in codes - e.g. UTF-n!).

I am still not clear on how it's going to work away from the test -
using the database situation, but I am HOPING it would work as
follows:
1. I will use 1 of these codes to read the file
2. then store the string into nvarchar field and add a note informing
users of the encoding I used
3. specify the same encoding when creating the file, after reading the
string from the db.

Do you think this would work?
Thanks again,
zoro.
Apr 1 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
9069
by: David Thomas | last post by:
Hi there, a while ago, I posted a question regarding reading japanese text from a text file. Well, since I solved the problem, I thought I'd post my solution for the benefit of other people with the same problem. The plan was to make a script to read and display japanese text. I will use it for making a japanese proverb script and for a japanese language study script.
3
7773
by: hunterb | last post by:
I have a file which has no BOM and contains mostly single byte chars. There are numerous double byte chars (Japanese) which appear throughout. I need to take the resulting Unicode and store it in a DB and display it onscreen. No matter which way I open the file, convert it to Unicode/leave it as is or what ever, I see all single bytes ok, but double bytes become 2 seperate single bytes. Surely there is an easy way to convert these mixed...
11
7039
by: Ron | last post by:
Hello, I'm having an aggravating time getting the "html" spewed by Word 2003 to display correctly in a webpage. The situation here is that the people creating the documents only know Word, and aren't very computer savvy. I created a system where they can save their Word documents as "html" and upload them to a certain directory, and the web page dynamically runs them through tidylib using the tidy extension to php4, thus causing the...
7
2264
by: Naren | last post by:
Hello All, Can any one help me in this file read problem. #include <stdio.h> int main() {
10
2545
by: Tibby | last post by:
I need to read/write not only text files, but binary as well. It seems like on binary files, it doesn't right the last 10% of the file. -- Thanks --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.538 / Virus Database: 333 - Release Date: 11/10/2003
3
5126
by: Chip | last post by:
There is surprisingly little information on the various encoding options for reading a text file. I have what seems to be a very basic issue: I'm reading a text file that includes Spanish characters such as "ñ". When I read the file into a string, that character is missing. Encoding seems to be the culprit. File writers SHOULD begin a file with the BOM (Byte Order Mark) to let us know what encoding to read the file with, but most software...
8
27537
by: Zephyre | last post by:
I have some UTF-8 text files written in Chinese to be read. Now the only method that I know to read text from it is to use fopen() function. Thus, I must read the contents byte by byte, change the UTF-8 characters to Unicode, store the characters into wchar_t variables. But I think this method is too complex and isn't elegant at all. Are there any ways to read the UTF-8 text files as simple and convenient as the way that we read ANSI...
3
18961
by: nicolasg | last post by:
Hi, I'm trying to open a file (any file) in binary mode and save it inside a new text file. After that I want to read the source from the text file and save it back to the disk with its original form. The problem is tha the binary source that I extract from the text file seems to be diferent from the source I saved. Here is my code: 1) handle=file('image.gif','rb')
11
5129
Dormilich
by: Dormilich | last post by:
Lately I have seen so much awful HTML, that I like to show what a HTML document should look like, regarding the requirements from the W3C. the absolute minimum is defined as: or expressed in code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <HTML> <HEAD> <TITLE>My first HTML document</TITLE>
0
9506
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9317
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10090
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9924
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8759
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5190
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5343
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3844
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2719
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.