ReadLine() from multiline string variable

G

Guest

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)
{...
 
B

Bruce Wood

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)
{...

Try looking into the StringReader class.
 
L

Linda Liu [MSFT]

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.
 
G

Guest

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

Guest

sam01m said:
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?

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
 
G

Guest

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top