Home > Back-end >  Get and Read from XML in C#
Get and Read from XML in C#

Time:09-05

I have log files where the name of the file is today's date. and I need to get the errors to attributes in these files if they are between the start and stop times currently kept in the variable. how can i achieve this?

<ErrorLogs>
  <Logs Hour_Date="9:34/1_6_2021" ID="1">
    <ErrorGUI>checkbox</ErrorGUI>
    <ErrorType>uplink fail</ErrorType>
    <ProgramPage>Failures Page</ProgramPage>
    <ErrorStartTime>9:34:20</ErrorStartTime>
  </Logs>
<ErrorLogs>

CodePudding user response:

As @JonSkeet mentioned you need to deserialize from XML to object classes. Deserialize the XML as ErrorLogs.

[XmlRoot("ErrorLogs")]
public class ErrorLogs
{
    [XmlElement("Logs")]
    public List<Log> Logs { get; set; } 
}

public class Log
{
    [XmlAttribute("Hour_Date")]
    public string HourDate { get; set; }

    [XmlAttribute("ID")]
    public string ID { get; set; }

    [XmlElement("ErrorGUI")]
    public string ErrorGUI { get; set; }

    [XmlElement("ErrorType")]
    public string ErrorType { get; set; }

    [XmlElement("ProgramPage")]
    public string ProgramPage { get; set; }

    [XmlIgnore]
    public TimeSpan ErrorStartTime { get { return TimeSpan.Parse(_ErrorStartTime); } }
    
    [XmlElement("ErrorStartTime")]
    public string _ErrorStartTime { get; set; }
}

Next, working with System.Linq to filter data by date range.

using System.Xml;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.IO;
using System.Linq;

XmlSerializer serializer = new XmlSerializer(typeof(ErrorLogs));

ErrorLogs errorLogs = (ErrorLogs)serializer.Deserialize(new StringReader(xml));

TimeSpan startTs = new TimeSpan(8, 0, 0); // Your Start Time
TimeSpan endTs = new TimeSpan(10, 0, 0); // Your End Time

var result = errorLogs.Logs
    .Where(x => startTs <= x.ErrorStartTime
            && x.ErrorStartTime <= endTs)
    .ToList();

Demo @ .NET Fiddle

CodePudding user response:

Try following :

using System;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
using System.Globalization;

namespace ConsoleApp2
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            List<ErrorLog> errors = new List<ErrorLog>();

            DateTime startDate = new DateTime(2021, 1, 1);
            DateTime endDate = new DateTime(2021, 12, 31);
            //create a loop to read all files
            errors.AddRange(ErrorLog.GetErrors(FILENAME, startDate, endDate));
 
            
        }

    }
    public class ErrorLog
    {
        public string errorGui { get; set; }
        public string errorType { get; set; }
        public string programPage { get; set; }
        public TimeSpan errorStartTime { get; set; }
        public DateTime date { get; set; }
        public int ID { get; set; }

        static string dateFormat = "H:mm/M_d_yyyy";
        static string timeFormat = "h:mm:ss";

        public static List<ErrorLog> GetErrors(string filename, DateTime start, DateTime end)
        {
            List<ErrorLog> errors = null;
            XDocument doc = XDocument.Load(filename);
            List<XElement> logs = doc.Descendants("Logs").ToList();

            foreach(XElement log in logs)
            {
                string strDate = (string)log.Attribute("Hour_Date");
                DateTime date = DateTime.ParseExact(strDate, dateFormat, CultureInfo.InvariantCulture);
                if(date >= start && date <= end)
                {
                    ErrorLog error = new ErrorLog();
                    error.date = date;
                    error.ID = (int)log.Attribute("ID");
                    error.errorGui = (string)log.Element("ErrorGui");
                    error.errorType = (string)log.Element("ErrorType");
                    error.programPage = (string)log.Element("ProgramPage");
                    string errorStartTime = (string)log.Element("ErrorStartTime");
                    error.errorStartTime = DateTime.ParseExact(errorStartTime, timeFormat, CultureInfo.InvariantCulture).TimeOfDay;

                    if (errors == null) errors = new List<ErrorLog>();
                    errors.Add(error);

                }    
            }


            return errors;
        }
    }


}
 
  • Related