繁体   English   中英

从 C# 中的 XML 获取和读取

[英]Get and Read from XML in C#

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

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

正如@JonSkeet 提到的,您需要从 XML 反序列化到 object 类。 将 XML 反序列化为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; }
}

接下来,使用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();

演示 @ .NET 小提琴

尝试以下操作:

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
            errors.AddRange(ErrorLog.GetErrors(FILENAME, startTime, endTime));
 
            
        }

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


}
 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM