0

For parsing SDP information from an RTSP, currently I use default c# string functions (like loop the string line by line) and foreach/switch methods. Default SDP information comes from a device in this form:

v=0
o=mhandley 2890844526 2890842807 IN IP4 126.16.64.4
s=SDP Seminar
i=A Seminar on the session description protocol
u=http://www.cs.ucl.ac.uk/staff/M.Handley/sdp.03.ps
[email protected] (Mark Handley)
c=IN IP4 224.2.17.12/127
t=2873397496 2873404696
a=recvonly
m=audio 3456 RTP/AVP 0
m=video 2232 RTP/AVP 31
m=whiteboard 32416 UDP WB
a=orient:portrait

Im wondering is this string is query-able using LINQ or something instead of foreaching each line, switching the first character and then storing the rest as a value, because this method is very sensitive to malfunction and error (like when 2 attributes/params are on 1 line or the first character isn't accidentally the right one (e.g. with a space before)). Before I begin to cover every damn exception than can occur, I'm wondering if there's a technique to query a string for values/keys using LINQ.

3
  • What expected result you want to store? Commented Nov 1, 2012 at 9:45
  • You have three items for m key. How you suppose to handle that? Commented Nov 1, 2012 at 9:50
  • @CuongLe For the unique keys like v,o,s, I have specific variables. For * keys like m, as you see in the example above, I'd like to use lists. There are another type of key/value not in this example: Attributes. They are written as following: 'a=fmpt:RTP/96' So I'd like to query for attributes (a's), then query for the key (string before :) and values (string after :) Commented Nov 1, 2012 at 9:52

2 Answers 2

3
var query = text.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
                .Select(line => line.Split('='))
                .GroupBy(x => x[0], x => x[1])
                .Select(g => new { Key = g.Key, Values = g.ToList() });

This will return entries grouped by key with values as list.

Or this way (if Linq Lookup is OK for you):

var lookup = text.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
                 .Select(line => line.Split('='))
                 .ToLookup(x => x[0], x => x[1]);

Usage:

foreach (var value in lookup["m"])
    // use value
Sign up to request clarification or add additional context in comments.

1 Comment

This! A hundred-times! Thank you very much for this understandable example.
1

you undoubtedly can do it with linq, but a simple loop (perhaps using StringReader to get the lines) checking line.Length != 0 then looking and line[0] and line.Substring(1) is probably more appropriate:

static void Process(string input)
{
    using (var reader = new StringReader(input))
    {
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            if(line.Length == 0) continue;

            char first = line[0];
            string rest = line.Substring(1);
            // ... process this line
        }
    }
}

2 Comments

What about the case when 2 key/value pairs occur on one line? ReadLine would be useless in this case.
@user1450661 then you need to define what the correct behavior is in that case (which you have not yet done)... it sounds to me like that is an error scenario, and the user should not expect it to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.