static void Main(string[] args) { DataTable logging = new DataTable(); logging.Columns.Add("Date", typeof(DateTime)); logging.Columns.Add("Severity", typeof(string)); logging.Columns.Add("id", typeof(string)); logging.Columns.Add("ID", typeof(string)); logging.Columns.Add("Msg", typeof(string)); XDocument docLog = XDocument.Load(LOG_XML_FILENAME); foreach (XElement log in docLog.Descendants("Log")) { DateTime date = (DateTime)log.Attribute("Date"); string severity = (string)log.Attribute("Severity"); string id = (string)log.Attribute("id"); string ID = (string)log.Attribute("ID"); string msg = (string)log.Attribute("Msg"); logging.Rows.Add(new object[] { date, severity, id, ID, msg }); } XDocument docStatus = XDocument.Load(STATUS_XML_FILENAME); Logic logic = Logic.AND; foreach (XElement Statuscode in docStatus.Descendants("Statuscode")) { DataTable status = null; List <KeyValuePair <string, object> > searchFields = new List <KeyValuePair <string, object> >(); foreach (XElement trigger in Statuscode.Descendants("Trigger")) { string searchField = (string)trigger.Attribute("searchField"); string searchText = (string)trigger.Attribute("searchText"); switch (searchField) { case "Date": searchFields.Add(new KeyValuePair <string, object>(searchField, (DateTime)trigger.Attribute("searchText"))); break; default: searchFields.Add(new KeyValuePair <string, object>(searchField, (string)trigger.Attribute("searchText"))); break; } } switch (logic) { case Logic.AND: status = logging.AsEnumerable() .Where(x => searchFields.All(field => (field.Value.GetType() == typeof(DateTime)) ? x.Field <DateTime>(field.Key) == (DateTime)field.Value : x.Field <string>(field.Key) == (string)field.Value)) .CopyToDataTable(); break; case Logic.OR: status = logging.AsEnumerable() .Where(x => searchFields.Any(field => (field.Value.GetType() == typeof(DateTime)) ? x.Field <DateTime>(field.Key) == (DateTime)field.Value : x.Field <string>(field.Key) == (string)field.Value)) .CopyToDataTable(); break; } } }
static void Main(string[] args) { DataTable logging = new DataTable(); logging.Columns.Add("Date", typeof(DateTime)); logging.Columns.Add("Severity", typeof(string)); logging.Columns.Add("id", typeof(string)); logging.Columns.Add("ID", typeof(string)); logging.Columns.Add("Msg", typeof(string)); XDocument docLog = XDocument.Load(LOG_XML_FILENAME); foreach (XElement log in docLog.Descendants("Log")) { DateTime date = (DateTime)log.Attribute("Date"); string severity = (string)log.Attribute("Severity"); string id = (string)log.Attribute("id"); string ID = (string)log.Attribute("ID"); string msg = (string)log.Attribute("Msg"); logging.Rows.Add(new object[] { date, severity, id, ID, msg }); } XDocument docStatus = XDocument.Load(STATUS_XML_FILENAME); foreach (XElement Statuscode in docStatus.Descendants("Statuscode")) { DataTable status = logging.Clone(); foreach (XElement trigger in Statuscode.Descendants("Trigger")) { string searchField = (string)trigger.Attribute("searchField"); string searchText = (string)trigger.Attribute("searchText"); switch (searchField) { case "Date": logging.AsEnumerable().Where(x => x.Field <DateTime>(searchField) == (DateTime)trigger.Attribute(searchText)).CopyToDataTable(status, LoadOption.Upsert); break; default: logging.AsEnumerable().Where(x => x.Field <string>(searchField) == searchText).CopyToDataTable(status, LoadOption.Upsert); break; } } } }
private void button1_Click(object sender, EventArgs e) { DataTable errorLogDatatable = new DataTable(); errorLogDatatable.Columns.Add("Date", typeof(DateTime)); errorLogDatatable.Columns.Add("Severity", typeof(string)); errorLogDatatable.Columns.Add("id", typeof(string)); errorLogDatatable.Columns.Add("ID", typeof(string)); errorLogDatatable.Columns.Add("Msg", typeof(string)); XDocument docLog = XDocument.Load(LOG_XML_FILENAME); foreach (XElement log in docLog.Descendants("Log")) { DateTime date = (DateTime)log.Attribute("Date"); string severity = (string)log.Attribute("Severity"); string id = (string)log.Attribute("id"); string ID = (string)log.Attribute("ID"); string msg = (string)log.Attribute("Msg"); errorLogDatatable.Rows.Add(new object[] { date, severity, id, ID, msg }); } XDocument docStatus = XDocument.Load(STATUS_XML_FILENAME); Logic logic = Logic.AND; foreach (XElement Statuscode in docStatus.Descendants("Statuscode")) { StatusCode statusCode = new StatusCode() { Code = (string)Statuscode.Attribute("value"), Text = (string)Statuscode.Attribute("text") }; statusCodes.Add(statusCode); DataTable status = null; foreach (XElement trigger in Statuscode.Descendants("Trigger")) { string searchField = (string)trigger.Attribute("searchField"); string searchText = (string)trigger.Attribute("searchText"); statusCode.Triggers.Add(new Trigger() { SearchField = searchField, SearchText = (string)trigger.Attribute("searchText") }); } switch (logic) { case Logic.AND: status = errorLogDatatable.AsEnumerable() .Where(x => statusCode.Triggers.All(field => x.Field <string>(field.SearchField) == field.SearchText)) .CopyToDataTable(); break; case Logic.OR: status = errorLogDatatable.AsEnumerable() .Where(x => statusCode.Triggers.Any(field => x.Field <string>(field.SearchField) == field.SearchText)) .CopyToDataTable(); break; } var bindingSource = new BindingSource { DataSource = status, }; dataGridView1.DataSource = bindingSource; } }