Bob
Bob

Reputation: 13

XML file to List

I am reading from an xml file that contains setting for my app. I want to read the xml file and convert it to a List<T>. The XML date is being retrieved correctly at var doc = XDocument.... but it doesn't populate the list and I cant figure out what is wrong.

thank you in advance.

<xPing>
  <PingInterval>3000</PingInterval>
  <TimeOut>1000</TimeOut>
  <MaxReplyTime>18</MaxReplyTime>
  <LogFileName>C:\Temp\LogFile.txt</LogFileName>
  <MaxFileSize>2</MaxFileSize>
  <LogFileFull>false</LogFileFull>
  <StopPing>false</StopPing>
  <EmailEnabled>true</EmailEnabled>
  <SMTPServer>mail.shaw.ca</SMTPServer>
  <PortNumber>25</PortNumber>
  <EmailSender>[email protected]</EmailSender>
  <EmailReceipient>[email protected]</EmailReceipient>
  <SendLogFileFull>false</SendLogFileFull>
  <ConsecutivePingsEnabled>true</ConsecutivePingsEnabled>
  <EmailConsecutivePing>5</EmailConsecutivePing>
</xPing>
public class xPing
{
    public string PingInterval { get; set; }
    public string TimeOut { get; set; }
    public string MaxReplytime { get; set; }
    public string LogFileName { get; set; }
    public string MaxFileSize { get; set; }
    public string LogFileFull { get; set; }
    public string StopPing { get; set; }        
    public string EmailEnabled { get; set; }
    public string SMTPServer { get; set; }
    public string PortNumber { get; set; }
    public string EmailSender { get; set; }
    public string EmailReceipient { get; set; }
    public string SendLogFileFull { get; set; }
    public string ConsecutivePingsEnabled { get; set; }
    public string EmailConsecutivePing { get; set; }
    
}

public class getSettings
{
    public List<xPing> settings()
    {
        var doc = XDocument.Load(AppDomain.CurrentDomain.BaseDirectory + "Settings.xml");

        var settings = doc.Root.Descendants("xPing").Select(node => new xPing 
        {
            PingInterval = node.Element("PingInterval").Value,
            TimeOut = node.Element("TimeOut").Value,
            MaxReplytime = node.Element("MaxReplytime").Value,
            LogFileName = node.Element("LogFileName").Value,
            MaxFileSize = node.Element("MaxFileSize").Value,
            LogFileFull = node.Element("LogFileFull").Value,
            StopPing = node.Element("StopPing").Value,
            EmailEnabled = node.Element("EmailEnabled ").Value,
            SMTPServer = node.Element("SMTPServer").Value,
            PortNumber = node.Element("PortNumber").Value,
            EmailSender = node.Element("EmailSender").Value,
            EmailReceipient = node.Element("EmailReceipient").Value,
            SendLogFileFull = node.Element("SendLogFileFull").Value,
            ConsecutivePingsEnabled = node.Element("ConsecutivePingsEnabled").Value,
            EmailConsecutivePing = node.Element("EmailConsecutivePing").Value,                
    }).ToList();
                    
        return settings;
    }
}

Upvotes: 1

Views: 54

Answers (1)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236248

You have several problems:

  • xPing is not a descendant element of a root - it's a root element of your xml file
  • There is only one root element, so you should not return a list
  • whitespace in EmailEnabled element name

Here is fixed parsing:

var node = xdoc.Root;
var settings = new xPing
{
    PingInterval = (string)node.Element("PingInterval"),
    TimeOut = (string)node.Element("TimeOut"),
    MaxReplytime = (string)node.Element("MaxReplytime"),
    LogFileName = (string)node.Element("LogFileName"),
    MaxFileSize = (string)node.Element("MaxFileSize"),
    LogFileFull = (string)node.Element("LogFileFull"),
    StopPing = (string)node.Element("StopPing"),
    EmailEnabled = (string)node.Element("EmailEnabled"),
    SMTPServer = (string)node.Element("SMTPServer"),
    PortNumber = (string)node.Element("PortNumber"),
    EmailSender = (string)node.Element("EmailSender"),
    EmailReceipient = (string)node.Element("EmailReceipient"),
    SendLogFileFull = (string)node.Element("SendLogFileFull"),
    ConsecutivePingsEnabled = (string)node.Element("ConsecutivePingsEnabled"),
    EmailConsecutivePing = (string)node.Element("EmailConsecutivePing")
};

Note: you can deserialize your settings from this xml file to avoid manual mapping of each field

var reader = XmlReader.Create(fileName);
var serializer = new XmlSerializer(typeof(xPing));
var settings = (xPing)serializer.Deserialize(reader);

Or you can easily read it with Microsoft.Extensions.Configuration.Xml and Microsoft.Extensions.Configuration.Binder nuget packages:

var config = new ConfigurationBuilder().AddXmlFile(fileName).Build();
var settings = config.Get<xPing>();

Upvotes: 1

Related Questions