Created a class to help pass values from an xml:
class ApptDetails
{
// public string date { get; set; }
public string starttime { get; set; }
public string endtime { get; set; }
public string details { get; set; }
public string notes { get; set; }
}
Using this XML format:
<March>
<date>
03/09/2024
<starttime>8:00 AM</starttime>
<endtime>8:30 AM</endtime>
<details>blah1</details>
<notes>blarty1</notes>
</date>
<date>
03/09/2024
<starttime>9:00 AM</starttime>
<endtime>9:30 AM</endtime>
<details>blah2</details>
<notes>blarty2</notes>
</date>
<date>
03/09/2024
<starttime>10:00 AM</starttime>
<endtime>10:30 AM</endtime>
<details>it finally freakin works!!!!</details>
<notes>Can i get an AMEN?</notes>
</date>
</March>
I couldn't even begin to describe all the things I have tried here. I have been researching this for over a week now, and have tried over a dozen different approaches. This is where I am at now, and is a bit of a culmination of what I have learned so far. Right now I am just sending the class variables to a messagebox for testing. The issue is the values are not passing to the object.
if (File.Exists(@"C:\FSAppointmentsTest\Appointments\" + now.ToString("MMMM") + ".xml"))
{
ApptDetails apptDetails = new ApptDetails();
XDocument appointments = XDocument.Load(@"C:\FSAppointmentsTest\Appointments\" + now.ToString("MMMM") + ".xml");
foreach (var child in appointments.Descendants("starttime"))
{
// XDocument appointmentData = XDocument.Parse("date");
IEnumerable<ApptDetails> result = from date in appointments.Descendants("starttime")
select new ApptDetails()
{
starttime = (string)date.Attribute("starttime").Value.ToString(),
endtime = (string)date.Attribute("endtime").Value.ToString(),
details = (string)date.Attribute("details").Value.ToString(),
notes = (string)date.Attribute("notes").Value.ToString(),
};
System.Windows.Forms.MessageBox.Show(apptDetails.starttime);
System.Windows.Forms.MessageBox.Show(apptDetails.endtime);
System.Windows.Forms.MessageBox.Show(apptDetails.details);
System.Windows.Forms.MessageBox.Show(apptDetails.notes);
}
}
Apologies if I missed anything. I have been reading from here for a long time but it is my first actual plea for help.
Try following
using System;
using System.Linq;
using System.Collections.Generic;
using System.Data;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApp10
{
class ApptDetails
{
public DateTime date { get; set; }
public DateTime starttime { get; set; }
public DateTime endtime { get; set; }
public string details { get; set; }
public string notes { get; set; }
public ApptDetails(XElement element)
{
date = DateTime.Parse(element.FirstNode.ToString());
starttime = (DateTime)element.Element("starttime");
endtime = (DateTime)element.Element("starttime");
details = (string)element.Element("details");
notes = (string)element.Element("notes");
}
}
class Program
{
static string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
List<ApptDetails> details = doc.Descendants("date").Select(x => new ApptDetails(x)).ToList();
}
}
}