linq 在C#中取得和读取XML

2hh7jdfx  于 2022-12-06  发布在  C#
关注(0)|答案(2)|浏览(210)

我有日志文件的文件名是今天的日期。我需要得到这些文件中的属性的错误,如果他们之间的开始和停止时间目前保存在变量。我如何才能做到这一点?

<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>
gc0ot86w

gc0ot86w1#

正如@JonSkeet提到的,需要将XML反序列化为对象类。

[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; }
}

接下来,使用 System.Linq 按日期范围筛选数据。

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

kdfy810k

kdfy810k2#

请尝试以下操作:

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>();

            TimeSpan startTime = new TimeSpan(8, 0, 0);
            TimeSpan endTime = new TimeSpan(19, 0, 0);
            //create a loop to read all files
            List<ErrorLog> results = ErrorLog.GetErrors(FILENAME, startTime, endTime);
            if (results != null)
            {
                errors.AddRange(results);
            }
        }
    }
    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, TimeSpan start, TimeSpan end)
        {
            List<ErrorLog> errors = null;
            XDocument doc = XDocument.Load(filename);
            List<XElement> logs = doc.Descendants("Logs").ToList();

            foreach(XElement log in logs)
            {
                string errorStartTime = (string)log.Element("ErrorStartTime");
                TimeSpan time = DateTime.ParseExact(errorStartTime, timeFormat, CultureInfo.InvariantCulture).TimeOfDay;

                if (time >= start && time <= end)
                {
                    ErrorLog error = new ErrorLog();
                    error.ID = (int)log.Attribute("ID");
                    error.errorGui = (string)log.Element("ErrorGui");
                    error.errorType = (string)log.Element("ErrorType");
                    error.programPage = (string)log.Element("ProgramPage");
                    error.errorStartTime = time;
                    string strDate = (string)log.Attribute("Hour_Date");
                    error.date = DateTime.ParseExact(strDate, dateFormat, CultureInfo.InvariantCulture);
                    if (errors == null) errors = new List<ErrorLog>();
                    errors.Add(error);

                }    
            }

            return errors;
        }
    }

}

相关问题