Попробуйте следующий метод:
static void Main(string[] args)
{
Console.WriteLine("Application Name Service Instance Name Machine Name Status");
string data = File.ReadAllText(@"Path\Data.txt");
var appArray = data.Split("Application ",StringSplitOptions.RemoveEmptyEntries);
foreach (var app in appArray)
{
var appName = PullValue(app,"Name");
var serviceArray = app.Split("Service Instance ", StringSplitOptions.RemoveEmptyEntries);
var services = serviceArray.Skip(1).ToArray();
foreach(var item in services)
{
var serviceInstanceName = PullValue(item, "Name");
var machineName = PullValue(item, "Machine Name");
var status = PullValue(item, "Status");
Console.WriteLine(" {0} {1} {2} {3}",appName, serviceInstanceName, machineName, status);
}
}
}
private static string PullValue(string line, string key)
{
key = key + ": ";
int ndx = line.IndexOf(key, 0, StringComparison.InvariantCultureIgnoreCase);
if (ndx >= 0)
{
var i=line.IndexOf(" ");
int ndx2 = line.IndexOf(" ", ndx+key.Length+1, StringComparison.InvariantCultureIgnoreCase);
if (ndx2 == -1)
ndx2 = line.Length - 1;
return line.Substring(ndx + key.Length, ndx2 - ndx - key.Length).Trim();
}
return "";
}
Результат:
