XML 示例:
<AssignmentHistory>
<PublicTalkInfo>
<PublicTalkNumber>21</PublicTalkNumber>
<PublicTalkSpeaker>Name3</PublicTalkSpeaker>
<PublicTalkCongregation>Place3</PublicTalkCongregation>
</PublicTalkInfo>
<PublicTalkInfo>
<MeetingDate>2023-10-15</MeetingDate>
<PublicTalkNumber>21</PublicTalkNumber>
<PublicTalkSpeaker>Name</PublicTalkSpeaker>
<PublicTalkCongregation>Place</PublicTalkCongregation>
</PublicTalkInfo>
<PublicTalkInfo>
<MeetingDate>2023-11-15</MeetingDate>
<PublicTalkNumber>21</PublicTalkNumber>
<PublicTalkSpeaker>Name2 </PublicTalkSpeaker>
<PublicTalkCongregation>Place</PublicTalkCongregation>
</PublicTalkInfo>
</AssignmentHistory>
为了简化我把它删掉了。
public string[] GetHistoryForTalkOutline(string historyDatabase, int talknumber)
{
List<string> list = new List<string>();
try
{
XDocument doc = XDocument.Load(historyDatabase);
var ptInfo = doc
.Descendants("PublicTalkInfo")
.Where(p => p.Element("PublicTalkNumber").Value == talknumber.ToString()).ToList();
SimpleLog.Log(ptInfo.Count().ToString());
foreach (var pt in ptInfo)
{
var date = pt.Element("MeetingDate");
var speaker = pt.Element("PublicTalkSpeaker");
var cong = pt.Element("PublicTalkCongregation");
if (date != null && speaker != null && cong != null)
{
DateTime objectdate = DateTime.Parse(date.Value);
string histitem = objectdate.ToString("d") + ", ";
histitem += speaker.Value + " (";
histitem += cong.Value + ")";
list.Add(histitem);
}
}
return list.ToArray();
}
catch (Exception ex)
{
SimpleLog.Log(ex);
return null;
}
}
我确信代码可以简化。但是,如何构建最终的字符串列表,其中内容按日期降序排列?请注意,某些条目中没有字段。
您可以将它们保留为保留日期值的格式,而不是将所有值存储在字符串列表中。例如,使用元组:
稍后当您添加到列表时,请执行以下操作:
要按您需要的顺序返回值,请使用一些简单的 Linq:
也许可以尝试一些类似的事情
如果您希望没有会议日期的元素显示在列表顶部,您可以替换
DateTime.MinValue
->DateTime.MaxValue
也许,这样的事情应该有所帮助::