-1

I have a string that is formatted like this (the string is one continuous, no new line return)

 /CallDump/CallInfo/KVP[@Key='Group' and (@Value='Best Group')]:10,
 /CallDump/CallInfo/child::KVP[@Key='Dept' and (@Value='Customer Service' or @Value='Sales')]:240,
 compare(Recordings/Recording/Location, 'New York')=0:20,
 default:5,

I cannot seem to find a non complex way to convert it into a dictionary()

results would be something like this:

Key: /CallDump/CallInfo/KVP[@Key='Group' and (@Value='Best Group')] Value: 10

Key: compare(Recordings/Recording/Location, 'New York')=0 Value: 20

4

1 回答 1

4

Regex您可以使用LINQ轻松做到这一点:

var input = @"/CallDump/CallInfo/KVP[@Key='Group' and (@Value='Best Group')]:10,
 /CallDump/CallInfo/child::KVP[@Key='Dept' and (@Value='Customer Service' or @Value='Sales')]:240,
 compare(Recordings/Recording/Location, 'New York')=0:20,
 default:5,";

var expression = new Regex(@"(.+):(\d{1,3})");

var result = input.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
                  .Select(x => x.Trim())
                  .Select(x => expression.Match(x))
                  .Select(m => new { Key = m.Groups[1].Value, Value = byte.Parse(m.Groups[2].Value) })
                  .ToDictionary(x => x.Key, x => x.Value);

返回Dictionary<string, byte>四个元素。

于 2013-10-16T23:51:42.437 回答