Solved

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

Posted on 2013-01-30
6
621 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 81

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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

739 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