在 c# 中使用 Linq 将一组 xml 值传递给对象

问题描述 投票:0回答:1

创建了一个类来帮助从 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; }
}

使用此 XML 格式:

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

我什至无法开始描述我在这里尝试过的所有事情。我已经研究这个问题一个多星期了,并尝试了十几种不同的方法。这就是我现在所处的位置,也是我迄今为止所学到的知识的结晶。现在我只是将类变量发送到消息框进行测试。问题是值没有传递给对象。

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

如果我错过了什么,我深表歉意。我已经阅读这里很长时间了,但这是我第一次真正请求帮助。

c# xml linq-to-xml
1个回答
0
投票

尝试以下

using System;
using System.Linq;
using System.Collections.Generic;
using System.Data;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApp10
{
    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();
  

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

            }
        }
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.