ReadLine() from multiline string variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?c2FtMDFt?=

    ReadLine() from multiline string variable

    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<Field s>GetNodeData(s tring source, string node,
    Boolean ReadLine)
    {
    //Get the field mappings.
    List<Fieldsreco rd = GetFields(node) ;

    List<List<Field s>records = new List<List<Field s>>();

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

    while (sr.Peek() >= 0)
    {...


  • Bruce Wood

    #2
    Re: ReadLine() from multiline string variable

    On Jul 19, 7:42 pm, sam01m <sam...@newsgro ups.nospamwrote :
    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<Field s>GetNodeData(s tring source, string node,
    Boolean ReadLine)
    {
    //Get the field mappings.
    List<Fieldsreco rd = GetFields(node) ;
    >
    List<List<Field s>records = new List<List<Field s>>();
    >
    using (StreamReader sr = new StreamReader(so urce))
    {
    //this is the string of data to process
    string contents = sr.ReadLine();
    >
    while (sr.Peek() >= 0)
    {...
    Try looking into the StringReader class.

    Comment

    • =?Utf-8?B?c2FtMDFt?=

      #3
      RE: ReadLine() from multiline string variable


      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.

      ...........exam ple 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"

      ...........prob lem 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?


      Comment

      • =?UTF-8?B?R8O2cmFuIEFuZGVyc3Nvbg==?=

        #4
        Re: ReadLine() from multiline string variable

        sam01m wrote:
        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.
        >
        ..........examp le 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"
        >
        ..........probl em 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(in put)) {
        while ((line = reader.ReadLine ()) != null) {
        Console.WriteLi ne(line.Length. ToString());
        }
        }

        Output:
        3
        3
        3
        3
        1

        --
        Göran Andersson
        _____
        Göran Anderssons privata hemsida.

        Comment

        • =?Utf-8?B?c2FtMDFt?=

          #5
          RE: ReadLine() from multiline string variable

          Here is my code:

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

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

          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!

          Comment

          Working...