.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

StreamReader.ReadLine(), StreamWriter.WriteLine() encoding issue

Posted By:      Posted Date: October 27, 2010    Points: 0   Category :.NET Framework

I have an ANSI text file which I read and change and write back the changes. The file originally is stored as follows:


Fine. Using StreamReader ctor without encoding parameter (which is not the same as Encoding.Default ) works fine (read+write) but the result is:

Line1<0D 0A>
Line2<0D 0A>
LineN<0D 0A>

So my question is how to force WriteLine() to write new line as 0A and not 0D 0A . Btw, when viewing a text file with 0A new lines using Notepad it is not displayed correctly. Notepad++ displays it correctly. The file does not have a BOM!

When using Encoding.UTF7 new lines are written as 0A but special characters like é is written as +AOk- which is not was I want.

The trick Write(0x0A) also not works because it will be written as characters '1' and '0' ("10" dec == 0A hex).

How to do it correctly? Many thanks!

Programming is a kind of art but not all programmers are a

View Complete Post

More Related Resource Links

StreamReader ReadLine ... New and Improved! (Fixed Single CR/LF)


I have been tasked with importing a large number of old log files into a database. These files came from a Unix type server and therefore have scattered Cr and Lf's throughout. Of course anyone who has ever tried to use a streamreader.ReadLine method knows this plays havoc.


My First attempt was just to do a ReadToEnd, and then use the Replace method to filter out everything but the CrLf's... this worked outstanding.... until my file sizes got too large, and then I started getting Out of Memory Issues.


According to Google, the solution was to use a BinaryReader along with it's Peek method to look Read in a Character at a time until a CrLf was found... This also worked great .... However it was extremely slow.


For example, I did a test on a 1mb file. The ReadLine method processed the file (even though incorrectly) in 8 seconds... the Binary Reader (though Correct) took 48 seconds. Doesn't sound like much but when you are looking at Gigs worth of file, it was taking hours to process just 1 CD.


... So finally to the good stuff..... I knew ReadLine was the way to go...but h

StreamReader issue with flat file



Please observe this flat file image: http://screencast.com/t/YMpeI5pOlI

You will on the bottom of the text file, line numbers shown by MSVS 2005 that tell me that a line exists, yet with no TEXT along side it, the text file has too many carriage returns at the bottom. Lets call these BLANK ROWS of data in my text file.

The issue is when I import this data like (vbNET):

Dim oRead As StreamReader
Dim str As String
Dim rows(0) As String

oRead = New StreamReader(FullPath)
str = oRead.ReadToEnd()
str = str.Replace(vbCr, "")
rows = str.Split(ControlChars.Lf)

The rows array has entries within it for the BLANK lines I show in the image attached. And this mucks up my indexes when I manipulate that rows data.

Can I do a 'Replace' or something to clear these BLANK lines before they get into my rows array ??

At the moment I just clean up the text file before import, but I need a coding solution to make my life easier !

Any ideas ! What do you do ?

Hebrew/Arabic encoding from PHP issue



I am migrating data from a PHP based CRM (SugarCRM) to something I have built. The source and the target DB is MSSQL. So far, I have successfully migrated most of the data. The one thing I can't get through is a table that contains comments. Some of these comments are in hebrew and arabic. In the comments table in the DB those comments appear as gibberish but when I go on the SugarCRM site I can see the comments properly. When I try to view this table in a .NET app (silverlight/winform/etc...) I get the same gibberish I see in the DB. I tried converting from one encoding to another and did all sorts of tests but I can't seem to be able to read the content of this table through .net code.

Does anyone have an idea?

Any help will be greatly appreciated,


stringwriter and streamwriter..encoding?




when I do serialization my code looks like below. but problem is StringWriter makes it always UTF-16 and I need UTF-8. Simply I can replace it in string but I dont like to do this. I know that StreamWriter has encoding overload. on the other hand this code is used by multithreading and streamwriter isnt thread safe. what would be best option to encode my string. is xmltextWriter other option maybe? is there any small performance issue between all these 3?


thanks for your help


   Dim xmlSerializer As New XmlSerializer(GetType(itemTemplate))
    Dim stringWriter As New StringWriter
    xmlSerializer.Serialize(stringWriter, _object)
    Serialize = stringWriter.ToString()

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."

IsapiModule Error 500 Issue


I am about exhausted trying to figure out this error:

HTTP Error 500.0 - Internal Server Error

Description: The page cannot be displayed because an internal server error has occurred.

Error Code: 0x8007007b

Notification: ExecuteRequestHandler

Module: IsapiModule

Requested URL: http://localhost:80/ReportServer

Physical Path: c:\Program Files\Microsoft SQL Server\MSSQL.2\Reporting Services\ReportServer

When I enable Failed Request Tracing Rules and view the log output I am supposing that the actual problem is in the 2nd call where there are 2 backslashes before the actual filename in the specified path:


CALL_ISAPI_EXTENSION DllName="C:\Windows\Microsoft.NET\Framework\v2.0.50727\\aspnet_isapi.dll"


MVC2 issue - Stuck. Trying to create a cms with nested partials


I am currently trying to display multiple items on a single page. I am not sure as to how to tackle this. What I have is a database table that has all the page data in. I have this run as a PagesController and this works fine. However what I would like to do is, if say the Products page is selected via the menu, I want to pull back not only the Products page html but also get all the products, and even be able to pull them through via category. I was trying to use a partial view, but I can't get it to work. The same would be for other pages, having partial views to display the other content, ie if Gallery is selected, the page html for Gallery shows and it then can push to a partial view that pulls all the gallery images from the gallery db table and so on. 

Maybe I am tackling this the wrong way. I have the pages stored in the db to allow for a CMS system I have setup. Maybe I need to use models for all the other items(Products, Gallery, Videos and such) and use the partial for the page html?

Any help would be greatly appreciated as I am confused.

Encoding en Decoding HTML



I stored in a database a ntext "<b>Hello</b>";

But when  I read it from the database I see <b>Hello</b> instead of Hello.

In the html source I see this: &lt;b&gt;Hello&lt;/b&gt;

Any Idea?


xpath issue


Hi i am using xpath to loop through each node,

the xml is below i can get the value of "link" but i cant get the thumbnail value, have you got any ideas how i can do this, my code is below.



     <link>test 1</link>
     <media:thumbnail url="http://test.com/image.jpg" height="75" width="75" />



     <link>test 2</link>
     <media:thumbnail url="http://test.com/image.jpg" height="75" width="75" />


public void FindLinks(XPathNavigator p_xPathNav, DataTable dt)
            //run the XPath query
            XPathNodeIterator xPathIt = p_xPathNav.Select("//item");

            //use the

ASP.NET XML Writer Encoding Problem


Hi everybody,

I'm working on an ASP.NET application which writes xml sitemap to xml file. while I'm generating the loc element, I found that the writer converts the "&" in dynamic URLs to "&amp;" which I think it's not acceptable by Google. is there any way to get out of this?

Part of my code is as follow:

                writer.WriteString("/english/pages/pages.aspx?sid=" + drSections("SectionID").ToString + "&pageid=" + drPages("PageID").ToString + "")

and my output comes like :

<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

User validation Issue in xlsx file



I am validating user from Login.xlsx file. It was working fine by validating email and password from that file. For testing i got new file same contents of file but added some more user names.

So after that when i validating the user i am getting the following error "Data type mismatch in criteria expression".

But when i replaced the new to old one it is working fine. The contens of both files are the same. So why this kind of error happened.


Multiple Paramatized Threading issue


I need the funcitonality to have two methods run concurrently of each other and the parent thread (aka the method that makes these two calls) to halt until BOTH are done. After the threads are done, I need to perform logic on the returns of the method, so it is essential for both of them to be complete before the parent thread continues. I have searched the internet and tried varous attacks, and I always come up with the same result, this being the parent thread continues operation after the first thread finishes, leaving the second thread running.  Can someone show me a quick example of how I can do this. I have tried checking states of the threads, halting the threads, but I always end up with the same thing. Any help would be great. Thanks.

Data Truncation issue with Enterprise Library Logging WriteLog stored Proc


Hi ,

I'm using Enterprise Library Logging  feature for logging. The issue i am facing is when the Logging message is too large(more than 65534 chars) ,complete data  is not logged in the Formatted Mesage column which is  of data Type nText .

I am able insert complete data if i try inserting from Sql insert Query from sql management studio. Do i need to add any attributes to data base listener or do i need to change the sp.

 Is there any way to increase the WriteLog stored proc param size in EnterpriseLibrary.Logging config file ? . Please let me know.


Thanks In Advance.

Word Automation Issue in Windows Server 2008 Hosting



The problem I am posting here is that I was facing nearly 2 weeks around. Any body comes with this stuff please help.

Word Automation in sample ASP.NET(C#) application.

I am using Microsoft.Office.Inetrop.Word Assembly for automation. Here I am reading a XXX.dot template file and fill the contents with dynamic data.

When i am executing my code in localhost:someportnumber the automation is working fine and I could get expected result and when I am hosting in my inetmgr(Windows XP is my OS) it is also working fine.

But the problem is that when I am hosting in my production server(Windows Server 2008 Standard Edition) I am not able to perform automation and results in the following error.

Data: System.Collections.ListDictionaryInternal
Message: Word has encountered a problem.
Source: Microsoft Word

The code gets failed in the following line:

ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();

Document wordDoc = wordApp.Documents.Add(ref oTemplate, ref oFalse, ref oMissing, ref oMissing); // Error in this line

I cannot able to proceed further. Can anybody please help me in solving this issue?

Thank you.

With Regards,


If Statement Issue



This is very strange to me. I have spent 2hrs looking into this inside out , but dont seem to get it

I have an if statement that inserts data into the database. But i check to make sure the name is not entered twice. Now when the name is already in the db, then it should insert else put out a message saying its already in the db

My code works fine except that  when its not in the db, its inserts but still puts out the message that says "Name is already in use" Not sure if am not seeing something. But thought i did bring it to the forum. Here is my code...Pretty simple

Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        Dim objConnection As SqlConnection
        Dim objCommand As SqlCommand
        Dim strSQLQuery1 As String


        ' If there's nothing to Add for then don't search
        ' o/w build our SQL Query and execute it.
        If String.IsNullOrEmpty(txtAdd.Text) Or Leadbank.SelectedValue = "000" Then


Express Yourself: Encoding Videos Using Microsoft Expression Encoder 3 SDK


Video on the Web involves huge amounts of data. Learn the best way to manage and move that data with the help of Microsoft Expression Encoder 3.

Adam Miller

MSDN Magazine June 2010

Toolbox: Issue and Defect Tracking, Automate IT Tasks, and More


In this issue, issue and defect tracking, automating IT tasks, and more.

Scott Mitchell

MSDN Magazine December 2006

The XML Files: Web Services Encoding and More


Aaron Skonnard covers the difference between document/literal and rpc/encoded Web Services and the history behind them.

Aaron Skonnard

MSDN Magazine May 2003

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend