Solved

using streamReader to read file with special characters such as copyright, registered

Posted on 2013-01-30
6
598 Views
Last Modified: 2013-02-04
I am reading a text file using the StreamReader with lines that have special characters such as copyright or registered
trademark symbol.
In the Model the property CompDescription is a string and I assign to it
from the file, the following sentence -->
StatLock® Stabilization Device
but for some reason when I debug and go into immediate window I see
StatLock¿ Stabilization Device and later when I save to the DB I see
'StatLock? Stabilization Device'

Below is the code.

 
var detailReader = reader.AsEnumerable();
 dataImportModel.WorkOrderDetail = (from line in detailReader
 select new WorkOrderDetailModel
 {
 Company = Int16.Parse(line.Substring(0, 3).Trim()),
 WorkOrderID = line.Substring(3, 7).Trim(),
 Component = string.Format("{0}", line.Substring(109, 15).Trim()),
 CompDescription = line.Substring(172, 1).Trim().Equals("N") ? "DELETE" :
 Convert.ToString(string.IsNullOrEmpty(line.Substring(173, 30).Trim()) ? string.Empty :
 line.Substring(173, 30).Trim()) + "|" + Convert.ToString(string.IsNullOrEmpty(line.Substring(281, 150).Trim()) ? string.Empty :
 line.Substring(281, 150).Trim()),
 QtyPerKit = decimal.Parse(line.Substring(124, 16).Trim()),
 UoM = line.Substring(203, 2).Trim(),
 Disclaimers = string.Empty,
 Deviation = line.Substring(209, 1).Trim(),
 Sequence = line.Substring(431, 1).Trim(),
 LevelOneBOMFlg = line.Substring(432, 3).Trim(),
 ExcludeFlg = line.Substring(465, 1).Trim()
 }).ToList();
 using (var reader = new StreamReader(fileName))
                {
                    try
                    {
                        
                       //while (reader.Peek() >= 0)
                       //{
                       //         Console.WriteLine(reader.ReadLine());
                       //         string strRecord
                                
                       //}
                        
                        var detailReader = reader.AsEnumerable();
                        dataImportModel.WorkOrderDetail = (from line in detailReader
                                                           select new WorkOrderDetailModel
                                                           {
                                                               Company = Int16.Parse(line.Substring(0, 3).Trim()),
                                                               WorkOrderID = line.Substring(3, 7).Trim(),
                                                               Component = string.Format("{0}", line.Substring(109, 15).Trim()),
                                                               CompDescription = line.Substring(172, 1).Trim().Equals("N") ? "DELETE" :
                                                                                                                     Convert.ToString(string.IsNullOrEmpty(line.Substring(173, 30).Trim()) ? string.Empty :
                                                                                                                     line.Substring(173, 30).Trim()) + "|" + Convert.ToString(string.IsNullOrEmpty(line.Substring(281, 150).Trim()) ? string.Empty :
                                                                                                                     line.Substring(281, 150).Trim()),
                                                               QtyPerKit = decimal.Parse(line.Substring(124, 16).Trim()),
                                                               UoM = line.Substring(203, 2).Trim(),
                                                               Disclaimers = string.Empty,
                                                               Deviation = line.Substring(209, 1).Trim(),
                                                               Sequence = line.Substring(431, 1).Trim(),
                                                               LevelOneBOMFlg = line.Substring(432, 3).Trim(),
                                                               ExcludeFlg = line.Substring(465, 1).Trim()
                                                           }).ToList();
                    }

Open in new window

0
Comment
Question by:mbarazi
6 Comments
 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
ID: 38839005
using (var reader = new StreamReader(Filename, System.Text.Encoding.ANSI); // or UTF-8
0
 

Author Comment

by:mbarazi
ID: 38839694
Hi,

I Tried UTF-8 and still it shows StatLock? Stabilization Device and ANSI is not available as an option.
The following is what it should show in debugger  
StatLock® Stabilization Device
0
 
LVL 23

Accepted Solution

by:
Roopesh Reddy earned 500 total points
ID: 38839859
Hi,

Did you tried Encoding.Default ?

Hope it helps u...
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 16

Expert Comment

by:hjgode
ID: 38840681
The encoding to use for streamreader depends on the encoding used in the text file. Do you know the encoding of the text file?

The encoding used in the VS output window is Unicode. An unicode encoded file should start with a BOM (byte order marker) but also UTF-8 etc can have BOM: http://en.wikipedia.org/wiki/Byte_order_mark

If the file is ANSI that will be now 1252 encoding: see http://msdn.microsoft.com/en-us/library/ms404377.aspx

Encoding and decoding extended chars into databases depends on the correct encoding setting of the database and the encoding used during add/read data. If the database is prepared for 1252 encoding and you store Unicode data but then read it back, you will get the wrong mapping.
0
 
LVL 23

Expert Comment

by:Roopesh Reddy
ID: 38849922
Hi,

Is the issue solved?
0
 

Author Closing Comment

by:mbarazi
ID: 38852636
That did Work, thanks for the help.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now