Could anybody help me to extract only the text field from the below JSON (response from my program) using c#
[{"unMeta":{}},[{"t":"Plain","c":[{"t":"Str","c":"{\"language\":\"en\",\"textAngle\":0.0,\"orientation\":\"Up\",\"regions\":[{\"boundingBox\":\"7,7,476,264\",\"lines\":
[{\"boundingBox\":\"7,7,476,58\",\"words\":
[{\"boundingBox\":\"7,7,42,44\",\"text\":\"If\"},{\"boundingBox\":\"62,16,283,49\",\"text\":\"computers\"},{\"boundingBox\":\"361,9,122,45\",\"text\":\"can't\"}]},
{\"boundingBox\":\"7,77,451,57\",\"words\":
[{\"boundingBox\":\"7,77,149,56\",\"text\":\"adapt\"},{\"boundingBox\":\"172,77,155,57\",\"text\":\"easily,\"},{\"boundingBox\":\"338,79,120,43\",\"text\":\"then\"}]},
{\"boundingBox\":\"8,146,460,57\",\"words\":
[{\"boundingBox\":\"8,146,178,56\",\"text\":\"maybe\"},{\"boundingBox\":\"201,147,82,44\",\"text\":\"the\"},{\"boundingBox\":\"299,148,169,55\",\"text\":\"people\"}]},
{\"boundingBox\":\"7,214,414,57\",\"words\":
[{\"boundingBox\":\"7,214,145,57\",\"text\":\"using\"},{\"boundingBox\":\"166,216,137,43\",\"text\":\"them\"},{\"boundingBox\":\"318,231,103,29\",\"text\":\"can.\"}]}]}]}"}]}]]
I am using the below code but response .regions throws error* i need to extract only the text field from the above JSON. I need to loop through the nodes
static async Task readJsonOutput(string response)
{
StringBuilder stringBuilder = new StringBuilder();
if (response != null && **response.Regions** != null)
{
foreach (var item in response.Regions)
{
foreach (var line in item.Lines)
{
foreach (var word in line.Words)
{
stringBuilder.Append(word.Text);
stringBuilder.Append(" ");
}
stringBuilder.AppendLine();
}
stringBuilder.AppendLine();
}
}
string result = stringBuilder.ToString();
//return stringBuilder.ToString();
}
}
public class Region
{
public string BoundingBox { get; set; }
public List<Line> Lines { get; set; }
}
public class Line
{
public string BoundingBox { get; set; }
public List<Word> Words { get; set; }
}
public class Word
{
public string BoundingBox { get; set; }
public string Text { get; set; }
}
Part of the problem is the JSON is double serialized. So you need to parse it, then get the "real" JSON from innermost c property and parse that part a second time. Then you can extract the text properties from there.
Using Json.Net's LINQ-to-JSON API, you can do it like this:
var innerJson = (string)JToken.Parse(json).SelectTokens("$..c").Last();
var words = JToken.Parse(innerJson).SelectTokens("$..text").Select(t => (string)t);
var text = string.Join(" ", words);
Demo here: https://dotnetfiddle.net/UvBRqv
Related
So I am using TDAmeritrade API to receive stock data with a C# Winforms program on Visual Studio. It takes the user input stock symbol and searches for the info. I am using HttpClient and Newtonsoft.Json and have been able to successfully perform the GET request and receive a JSON string back, but I do not know how to get all of the information I need out of it.
Here is the JSON:
https://drive.google.com/file/d/1TpAUwjyqrHArEXGXMof_K1eQe0hFoaw5/view?usp=sharing
Above is the JSON string sent back to me then formatted. My goal is to record information for each price in "callExpDateMap.2021-02-19:11" and "callExpDateMap.2021-03-19:39". The problem is that for each different stock, the dates that show up in "callExpDateMap" are going to be different.
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
var response = await client.GetAsync(url);
var info = await response.Content.ReadAsStringAsync();
dynamic config = JsonConvert.DeserializeObject<dynamic>(info, new ExpandoObjectConverter());
return config;
This is the code I have right now. I know the last for statement is not correct. How can I parse to the specific sections I want (callExpDateMap.expirationdate.StrikePrice) and get the information needed from each without knowing the dates and Strike prices beforehand? Is there a way to innumerate it and search through the JSON as if it were all a bunch of arrays?
The code below is perhaps not the most elegant nor complete, but I think it will get you going. I would start by using the JObject.Parse() from the Newtonsoft.Json.Linq namespace and take it from there.
JObject root = JObject.Parse(info);
string symbol = root["symbol"].ToObject<string>();
foreach (JToken toplevel in root["callExpDateMap"].Children())
{
foreach (JToken nextlevel in toplevel.Children())
{
foreach (JToken bottomlevel in nextlevel.Children())
{
foreach (JToken jToken in bottomlevel.Children())
{
JArray jArray = jToken as JArray;
foreach (var arrayElement in jArray)
{
InfoObject infoObject = arrayElement.ToObject<InfoObject>();
Console.WriteLine(infoObject.putCall);
Console.WriteLine(infoObject.exchangeName);
Console.WriteLine(infoObject.multiplier);
}
}
}
}
}
public class InfoObject
{
public string putCall { get; set; }
public string symbol { get; set; }
public string description { get; set; }
public string exchangeName { get; set; }
// ...
public int multiplier { get; set; }
// ...
}
This is official documentation of Newtonsoft method you are trying to use.
https://www.newtonsoft.com/json/help/html/Overload_Newtonsoft_Json_JsonConvert_DeserializeObject.htm
If an API's method returns different json propeties and you cannot trust it's property names all the times, then you can try using a deserialize method that returns .Net object, for example: JsonConvert.DeserializeObject Method (String)
https://www.newtonsoft.com/json/help/html/M_Newtonsoft_Json_JsonConvert_DeserializeObject.htm
That method's signature is this:
public static Object DeserializeObject(string value)
Parameter is: value of type json string.
Return Value is: Object of type object.
If you do not want an Object, then you can of course use a .Net type you have. Such as this method:
JsonConvert.DeserializeObject Method (String)
Any property that you have in both (the .net type and json object) will get populated. If .net type has properties that do not exist in json object, then those will be ignored. If json object has properties that do not exist in.net, then those will be ignored too.
Here's an example of a .Net type
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace MyNameSpace
{
public class TDAmeritradeStockData
{
[JsonProperty("symbol")]
public string Symbol { get; set; }
[JsonProperty("status")]
public string Status { get; set; }
[JsonProperty("callExpDateMap")]
public object CallExpDateMap { get; set; }
//...
//...
public CallExpDateMapType[] CallExpDateMapList { get; set; }
}
public class CallExpDateMapType
{
[JsonProperty("expirationdate")]
public string Expirationdate { get; set; }
[JsonProperty("StrikePrice")]
public List<StrikePriceType> StrikePriceList { get; set; }
}
public class StrikePriceType
{
public string StrikePrice { get; set; }
public List<StrikePricePropertiesType> StrikePricePropertiesList { get; set; }
}
public class StrikePricePropertiesType
{
[JsonProperty("putCall")]
public string PutCall { get; set; }
[JsonProperty("symbol")]
public string Symbol { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("exchangeName")]
public string ExchangeName { get; set; }
[JsonProperty("bid")]
public double Bid { get; set; }
[JsonProperty("ask")]
public double Ask { get; set; }
//...
//...
}
[TestClass]
public class TestTestTest
{
[TestMethod]
public void JsonTest()
{
var jsondata = ReadFile("data.json");
var model = JsonConvert.DeserializeObject<TDAmeritradeStockData>(jsondata);
JObject jObject = (JObject)model.CallExpDateMap;
var count = ((JObject)model.CallExpDateMap).Count;
model.CallExpDateMapList = new CallExpDateMapType[count];
var jToken = (JToken)jObject.First;
for (var i = 0; i < count; i++)
{
model.CallExpDateMapList[i] = new CallExpDateMapType
{
Expirationdate = jToken.Path,
StrikePriceList = new List<StrikePriceType>()
};
var nextStrikePrice = jToken.First.First;
while (nextStrikePrice != null)
{
var nextStrikePriceProperties = nextStrikePrice;
var srikePriceList = new StrikePriceType
{
StrikePrice = nextStrikePriceProperties.Path,
StrikePricePropertiesList = JsonConvert.DeserializeObject<List<StrikePricePropertiesType>>(nextStrikePrice.First.ToString())
};
model.CallExpDateMapList[i].StrikePriceList.Add(srikePriceList);
nextStrikePrice = nextStrikePrice.Next;
}
jToken = jToken.Next;
}
Assert.IsNotNull(model);
}
private string ReadFile(string fileName)
{
using (var fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
var data = new StringBuilder();
using (var streamReader = new StreamReader(fileStream))
{
while (!streamReader.EndOfStream) data.Append(streamReader.ReadLine());
streamReader.Close();
}
fileStream.Close();
return data.ToString();
}
}
}
}
It is needed to parse JSONString into List. (List of instances)
I'm trying to use JSON.NET by Newtonsoft.
I have classes:
public class Item
{
public int ID { get; set; }
public string Name { get; set; }
public string Url { get; set; }
public string Manufactorers { get; set; }
}
The JSON string looks something like this:
[
{
"Column0":23.0,
"Column1":"Евроен",
"Column2":"https://www.123.com",
"Column3":"Фак"
},
{
"Column0":24.0,
"Column1":"Еил",
"Column2":"https://www.123.com",
"Column3":"Старт"
}
]
I've been trying to do something like this:
string JSONString = string.Empty;
JSONString = JsonConvert.SerializeObject(result);
List<Item> items = JsonConvert.DeserializeObject<List<Item>>(JSONString);
But it returns 0 and null.
I have no idea, how to fix it.
Also here I truy to parse Excel file. This code works, but after deserialization, I have just 0 and null.
var filePath = #"..\..\..\..\doc.xlsx";
using (var steam = File.Open(filePath, FileMode.Open, FileAccess.Read))
{
using (var reader = ExcelReaderFactory.CreateReader(steam))
{
var result = reader.AsDataSet().Tables["Лист1"];
string JSONString = string.Empty;
JSONString = JsonConvert.SerializeObject(result);
List<Item> items = JsonConvert.DeserializeObject<List<Item>>(JSONString);
}
}
The naming of JSON and your class does not match. This can be fixed using JsonProperty attributes:
[JsonProperty("Column0")]
public decimal ID { get; set; }
Second, JSON deserizlizer can not deserialize string "23.0" to int when there is decimal point. You can retype ID to decimal or double to make it work.
Little test here:
public class TestClass
{
[JsonProperty("Column0")]
public decimal ID { get; set; }
}
Then the deserialization works without errors:
var testClassJson = "{\"Column0\": 12.0}";
var i = JsonConvert.DeserializeObject<TestClass>(testClassJson);
I am unable to deserialize the json result I am getting from google api during an API call. The API request is successful, but I can't deserialize it.
Code for deserialization is as follows :
var datareceived = JsonConvert.DeserializeObject<RootObject>(response);
Please find below the json I am getting from Google api(response object in above code) :
{"result":[]}
{"result":[{"alternative":[{"transcript":"distance between the trees","confidence":0.46962094},{"transcript":"Justin prescription that reason"},{"transcript":"Justin Swift accessories"},{"transcript":"justice respiratory"},{"transcript":"Justin syska accessories"}],"final":true}],"result_index":0}
Exception Details :
Message = "Additional text encountered after finished reading JSON content: {. Path '', line 2, position 0."
RootObject Class :
public class RootObject
{
public List<Result> result { get; set; }
public int result_index { get; set; }
}
Any help would be highly appreciated!
you just can't parse this JSON as it is because } symbolize the end of JSON
but you can do something like this
string json = "{\"result\":[]}\r\n{\"result\":[{\"alternative\":[{\"transcript\":\"distance between the trees\",\"confidence\":0.46962094},{\"transcript\":\"Justin prescription that reason\"},{\"transcript\":\"Justin Swift accessories\"},{\"transcript\":\"justice respiratory\"},{\"transcript\":\"Justin syska accessories\"}],\"final\":true}],\"result_index\":0}";
string trueJson = json.Split(new[] { Environment.NewLine }, StringSplitOptions.None)[1];
try
{
RootObject rootObject = JsonConvert.DeserializeObject<RootObject>(trueJson);
rootObject = rootObject;
}
catch (Exception ex)
{
throw;
}
public class Alternative
{
public string transcript { get; set; }
public double confidence { get; set; }
}
public class Result
{
public List<Alternative> alternative { get; set; }
public bool final { get; set; }
}
public class RootObject
{
public List<Result> result { get; set; }
public int result_index { get; set; }
}
You can utilize following method to get JSON in List:
public List<string> GetJsonItems(string jsonString)
{
int bracketCount = 0;
List<string> jsonItems = new List<string>();
StringBuilder Json = new StringBuilder();
foreach (char c in jsonString)
{
if (c == '{')
++bracketCount;
else if (c == '}')
--bracketCount;
Json.Append(c);
if (bracketCount == 0 && c != ' ')
{
jsonItems.Add(Json.ToString());
Json = new StringBuilder();
}
}
return jsonItems;
}
Then utilize it like:
List<string> strings = GetJsonItems(response.Replace(Environment.NewLine, String.Empty));
if (strings.Length > 1)
{
List<Rootobject> allResultSet = new List<Rootobject>();
foreach (var str in strings)
{
Rootobject items = JsonConvert.DeserializeObject<Rootobject>(str);
if (items.result.Length > 0)
{
allResultSet.Add(items);
}
}
}
It seems that each line of the result contains a separate JSON object. (It's not clear why the API doesn't return an actual JSON array instead.) If this is the case, you can split the response into lines and parse each line separately:
string[] lines = response.Split(new char[]{'\r','\n'},StringSplitOptions.RemoveEmptyEntries);
var datareceived1 = JsonConvert.DeserializeObject<RootObject>(lines[0]);
... and so on.
From this code. I want to parse only value from the json file
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
using (StreamReader file = File.OpenText(openFileDialog1.FileName))
using (JsonTextReader reader = new JsonTextReader(file))
{
while (reader.Read())
{
if (reader.Value != null)
{
richTextBox1.Text = reader.Value.ToString();
}
else
{
MessageBox.Show("Error while parsing json file. Please try again.");
}
}
}
}
And the value is
{
"install.and": "a",
"install.emailAddress": "E-mailová adresa",
"install.emailIncorrect": "Zadejte platnou e-mailovou adresu.",
"install.emailRetryPrefix": "Neobdrželi jste e-mail? Zkuste to znovu",
"install.emailRetry": "Zkuste to znovu",
"install.emailSend": "Odeslat odkaz",
"install.emailSent": "E-mail byl odeslán!",
"install.emailSentTo": "E-mail byl odeslán",
"install.emailText1": "Můžete navštívit",
"install.emailText2": "Pokud nám poskytnete e-mailovou adresu, budeme vám moci poslat odkaz na pozdější instalaci.",
"install.installing": "Instalace...",
"install.later": "Instalovat později",
"install.licenseAgreement": "licenční smlouva",
"install.privacyPolicy": "zásady ochrany osobních údajů",
"install.quit": "Ukončit instalační program"
}
I want to parse it after : symbol. (Is it value?) to show in richTextbox as Text.
Try this code
using (StreamReader file = File.OpenText(openFileDialog1.FileName))
using (JsonTextReader reader = new JsonTextReader(file))
{
var o = JObject.Load(reader);
foreach (var v in o)
{
var value = v.Value.Value<string>();
//do whatever you want with value
}
}
If you want only values joined by newline, then try this one
using (StreamReader file = File.OpenText(openFileDialog1.FileName))
using (JsonTextReader reader = new JsonTextReader(file))
{
var o = JObject.Load(reader);
var e = o.Values().Select(x => x.Value<string>());
var values = string.Join(Environment.NewLine, e);
//do whatever you want with values
}
Introduce two temporary variable to hold key and value
string key = string.Empty;
string value = string.Empty;
Modify your while loop like this,
using (JsonTextReader reader = new JsonTextReader(file))
{
while (reader.Read())
{
if (reader.Value != null)
{
key = reader.Value.ToString();
if (reader.Read())
value = reader.Value.ToString();
Console.WriteLine("{0} : {1}", key,value);
//Instead of writing in a console, process and write it in Rich text box.
}
}
}
You can use Json.Net and create a model :
public class JsonObject
{
[JsonProperty("install.and")]
public string install_and { get; set; }
[JsonProperty("install.emailAddress")]
public string emailAddress { get; set; }
[JsonProperty("install.emailIncorrect")]
public string emailIncorrect { get; set; }
[JsonProperty("emailRetryPrefix")]
public string emailRetryPrefix { get; set; }
[JsonProperty("install.emailRetry")]
public string emailRetry { get; set; }
[JsonProperty("install.emailSend")]
public string emailSend { get; set; }
[JsonProperty("install.emailSent")]
public string emailSent { get; set; }
[JsonProperty("install.emailSentTo")]
public string emailSentTo { get; set; }
[JsonProperty("install.emailText1")]
public string emailText1 { get; set; }
[JsonProperty("install.emailText2")]
public string emailText2 { get; set; }
[JsonProperty("install.installing")]
public string installing { get; set; }
[JsonProperty("install.later")]
public string later { get; set; }
[JsonProperty("install.licenseAgreement")]
public string licenseAgreement { get; set; }
[JsonProperty("install.privacyPolicy")]
public string privacyPolicy { get; set; }
[JsonProperty("install.quit")]
public string quit { get; set; }
}
Then you can prase you json file:
string json_data = "{\"install.and\": \"a\",\"install.emailAddress\": \"E-mailová adresa\",\"install.emailIncorrect\": \"Zadejte platnou e-mailovou adresu.\",\"install.emailRetryPrefix\": \"Neobdrželi jste e-mail? Zkuste to znovu\",\"install.emailRetry\": \"Zkuste to znovu\",\"install.emailSend\": \"Odeslat odkaz\",\"install.emailSent\": \"E-mail byl odeslán!\",\"install.emailSentTo\": \"E-mail byl odeslán\",\"install.emailText1\": \"Můžete navštívit\",\"install.emailText2\": \"Pokud nám poskytnete e-mailovou adresu, budeme vám moci poslat odkaz na pozdější instalaci.\",\"install.installing\": \"Instalace...\",\"install.later\": \"Instalovat později\",\"install.licenseAgreement\": \"licenční smlouva\",\"install.privacyPolicy\": \"zásady ochrany osobních údajů\",\"install.quit\": \"Ukončit instalační program\"";
JsonObject data = JsonConvert.DeserializeObject<JsonObject>(json_data);
richTextBox1.Text = data.emailAddress;
richTextBox2.Text = data.emailIncorrect;
richTextBox3.Text = data.emailRetry;
[...]
First, Install newtonsoft.json from nuget package manager. Add the namespace
using Newtonsoft.Json.Linq;
create a class to easily handle the values.
class Details
{
public string and;
public string EmailAddress;
public string EmailIncorrect;
public string EmailRetry;
public string EmailSend;
public string EmailSent;
}
Then read the file into a string and parse it using JObject.
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string file = File.ReadAllText(openFileDialog1.FileName);
JObject jo = JObject.Parse(file);
Details dt = new Details();
dt.and = (string)jo["install.and"];
richTextBox1.Text = reader.Value.ToString();
}
Looking for a good way to parse out of this text file, the values highlighted with the yellow boxes using C#. Each section is delineated by a TERM # which I forgot to highlight. Tried this:
string fileName = "ATMTerminalTotals.txt";
StreamReader sr = new StreamReader(fileName);
string[] delimiter = new string[] { " " };
while (!sr.EndOfStream)
{
string[] lines = sr.ReadLine().Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
foreach (string line in lines)
{
Console.WriteLine(line);
}
}
Console.ReadLine();
Safe to say I am reading lines correctly and removing "white spaces." Although, as an amateur to programming, not sure of a valid way to accurately "know" that I am getting the values from this report that I need. Any advice?
i've tested this with a very simple program to parse the given file,
basically i've created two basic classes, a page class holding a collection of terminal report class (the tran type rows)
these rows maybe even can be represented as transaction and a billing class too
first parsed the data, setting the parameters needed and lastly just accessing the properties
just rushed it to be as simple as possible, no error handling etc... its just to give you a sense of how id start solving these kind of tasks, hope it helps
Adam
namespace TerminalTest
{
class Program
{
public class TerminalReport
{
public string Word { get; set; }
public int Denials { get; set; }
public int Approvals { get; set; }
public int Reversals { get; set; }
public double Amount { get; set; }
public int ON_US { get; set; }
public int Alphalink { get; set; }
public int Interchange { get; set; }
public int Surcharged { get; set; }
public static TerminalReport FromLine(string line)
{
TerminalReport report = new TerminalReport();
report.Word = line.Substring(0, 11);
line = line.Replace(report.Word, string.Empty).Trim();
string[] split = line.Split(' ');
int i = 0;
// transaction summary
report.Denials = int.Parse(split[i++]);
report.Approvals = int.Parse(split[i++]);
report.Reversals = int.Parse(split[i++]);
report.Amount = double.Parse(split[i++]);
// billing counts
report.ON_US = int.Parse(split[i++]);
report.Alphalink = int.Parse(split[i++]);
report.Interchange = int.Parse(split[i++]);
report.Surcharged = int.Parse(split[i++]);
return report;
}
}
public class TerminalPage
{
public int PageNumber { get; set; }
public double TotalSurcharges { get; set; }
public List<TerminalReport> Rows { get; set; }
public TerminalPage(int num)
{
PageNumber = num;
Rows = new List<TerminalReport>();
}
public int TotalDenials
{
get
{
return rows.Sum(r => r.Denials);
}
}
public int TotalApprovals
{
get
{
return Rows.Sum(r => r.Approvals;
}
}
public int TotalReversals
{
get
{
return Rows.Sum(r => r.Reversals;
}
}
public double TotalAmount
{
get
{
return Rows.Sum(r => r.Amount);
}
}
public int TotalON_US
{
get
{
return Rows.Sum(r => r.ON_US);
}
}
public int TotalAlphalink
{
get
{
return Rows.Sum(r => r.Alphalink);
}
}
public int TotalInterchange
{
get
{
return Rows.Sum(r => r.Interchange);
}
}
public int TotalSurcharged
{
get
{
return Rows.Sum(r => r.Surcharged);
}
}
}
private static string CleanString(string text)
{
return Regex.Replace(text, #"\s+", " ").Replace(",", string.Empty).Trim();
}
private static List<TerminalPage> ParseData(string filename)
{
using (StreamReader sr = new StreamReader(File.OpenRead(filename)))
{
List<TerminalPage> pages = new List<TerminalPage>();
int pageNumber = 1;
TerminalPage page = null;
bool parse = false;
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
line = CleanString(line);
if (line.StartsWith("TRAN TYPE"))
{
// get rid of the ----- line
sr.ReadLine();
parse = true;
if (page != null)
{
pages.Add(page);
}
page = new TerminalPage(pageNumber++);
}
else if (line.StartsWith("="))
{
parse = false;
}
else if (line.StartsWith("TOTAL SURCHARGES:"))
{
line = line.Replace("TOTAL SURCHARGES:", string.Empty).Trim();
page.TotalSurcharges = double.Parse(line);
}
else if (parse)
{
TerminalReport r = TerminalReport.FromLine(line);
page.Rows.Add(r);
}
}
if (page != null)
{
pages.Add(page);
}
return pages;
}
}
static void Main(string[] args)
{
string filename = #"C:\bftransactionsp.txt";
List<TerminalPage> pages = ParseData(filename);
foreach (TerminalPage page in pages)
{
Console.WriteLine("TotalSurcharges: {0}", page.TotalSurcharges);
foreach (TerminalReport r in page.Rows)
Console.WriteLine(r.Approvals);
}
}
}
}
I'm not sure I'd split it by spaces actually.. the textfile looks like its split into columns. You might want to read like 10 chars (or whatever the width of the column is) at a time... and I'd parse the whole file into a dictionary so you get entries like
dict["WDL FRM CHK"]["# DENIALS"] = 236
then you can easily retrieve the values you want from there, and if you ever need more values in the future, you've got them.
Alternatively, you can use regexs. You can grab the first value with a regex like
^WDL FRM CHK\s+(?<denials>[0-9,]+)\s+(?<approvals>[0-9,]+)$
using
m.Groups["approvals"]
anyway I recommend you to wrap your StreamReader with using block:
using (StreamReader sr = new StreamReader(fileName))
{
// do stuff
}
Read more on MSDN
Given that it seems to have a standard, regular format, I would use regular expressions. You can check the starting code to figure out what row you're on, then an expression that will parse out the numbers and ignore whitespace will, very likely, be easier than handling it manually.
using System;
using System.Text.RegularExpressions;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Regex exp = new Regex(#"WDL FRM CHK(\s)+[1-9,]+(\s)+(?<approvals>[1-9,]+)(\s)+");
string str = "WDL FRM CHK 236 1,854 45,465 123 3";
Match match = exp.Match(str);
if (match.Success)
{
Console.WriteLine("Approvals: " + match.Groups["approvals"].Value);
}
Console.ReadLine();
}
}
}
Apdated from the following article to parse one of your numbers:
How to match a pattern by using regular expressions and Visual C#