Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / July 2007

Tip: Looking for answers? Try searching our database.

ReadLine() from multiline string variable

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
sam01m - 20 Jul 2007 03:42 GMT
just as the subject states, I need to find a way to read each line from a
multiline variable without having to write the string out to a file so I can
stream it in and use ReadLine(). Below is a snippet of code where I am trying
to read the string that has been passed into the function.

       //source is the string of data to parse out
       //nodes is the Xml Node Name in the mappingFile
("/Meters/Ion/Report/Field")
       //ReadLine indicates whether to process line by line, or read as
continuous string
       public List<List<Fields>> GetNodeData(string source, string node,
Boolean ReadLine)
       {
           //Get the field mappings.
           List<Fields> record = GetFields(node);

           List<List<Fields>> records = new List<List<Fields>>();

           using (StreamReader sr = new StreamReader(source))
           {
               //this is the string of data to process
               string contents = sr.ReadLine();

               while (sr.Peek() >= 0)
               {...
Bruce Wood - 20 Jul 2007 04:57 GMT
> just as the subject states, I need to find a way to read each line from a
> multiline variable without having to write the string out to a file so I can
[quoted text clipped - 21 lines]
>                 while (sr.Peek() >= 0)
>                 {...

Try looking into the StringReader class.
Linda Liu [MSFT] - 20 Jul 2007 08:03 GMT
Hi Sam,

I agree with Bruce. You could use the StringReader class for you purpose.

For more information on the StringReader class, you may refer to the
following MSDN document:

'StringReader Class'
http://msdn2.microsoft.com/en-us/library/system.io.stringreader.aspx

Hope this helps.

If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
sam01m - 20 Jul 2007 15:10 GMT
Excellent, thank you (both) for the information. the only problem I'm
finding with ReadLine() though, in either implementation (StreamReader or
StringReader), is that I'm finding it strips out trailing whitespace. I have
preformatted data to be of a very specific length, whereby sometimes the last
two spaces have values, but more often than not, they are blank.

..........example line of data:

" 13:00SI     137.76       11.37         .00        4.01  "

..........when read through ReadLine() evaluates like this:

" 13:00SI     137.76       11.37         .00        4.01"

..........problem is, sometimes the input data looks like this:

" 13:00SI     137.76AD     11.37AD       .00AD      4.01AD"

..........in any case, I need a fixed string length when I parse out each
ReadLine()

Any ideas?
Göran Andersson - 20 Jul 2007 16:36 GMT
> Excellent, thank you (both) for the information. the only problem I'm
> finding with ReadLine() though, in either implementation (StreamReader or
[quoted text clipped - 18 lines]
>
> Any ideas?

How have you determined that the ReadLine method removes the spaces?

I tried this code, and it shows that the spaces are not removed:

string input = "  a\r\na  \r\n a \r\na a\r\na";
using (StringReader reader = new StringReader(input)) {
    while ((line = reader.ReadLine()) != null) {
        Console.WriteLine(line.Length.ToString());
    }
}

Output:
3
3
3
3
1

Signature

Göran Andersson
_____
http://www.guffa.com

sam01m - 20 Jul 2007 21:56 GMT
Here is my code:

public void GetNodeData(string source, string node, Boolean ReadLine)
{
  using (StringReader sr = new StringReader(source))
           {
               string contents;

               if (ReadLine == true)
                  {
                      contents = sr.ReadLine();
                  }
    else
      {
          contents=sr.ReadToEnd();
      }

                   while (contents != null)
                   {

                        contents = sr.ReadLine();
                    }
           }
}

Here is the input string taken directly from the "source" variable at
runtime. Notice that each line contains two spaces just before \r:

" 11:00       147.02        8.24         .00        1.80  \r\n
 12:00       137.22        9.47         .00        3.29  \r\n
 13:00SI     137.76       11.37         .00        4.01  \r"

Here is the result of "contents = sr.ReadLine();"

" 11:00       147.02        8.24         .00        1.80  "
" 12:00       137.22        9.47         .00        3.29  "
" 13:00SI     137.76       11.37         .00        4.01  "

Hmmm.... I stand corrected, my apologies!

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.