C# Linq xml到对象-如何填充集合<;foo>;

C# Linq xml到对象-如何填充集合<;foo>;,c#,linq,C#,Linq,我需要阅读一些xml并填充我的类结构。如果有人能为我提供一些简洁的代码,我会非常高兴 我的简化班级结构: public class Event { [XmlAttribute("Id")] public string Id { get; set; } [XmlElement("StartTimes")] public Collection<StartTime> StartTimeCollection; } public class

我需要阅读一些xml并填充我的类结构。如果有人能为我提供一些简洁的代码,我会非常高兴

我的简化班级结构:

public class Event
{
    [XmlAttribute("Id")]
    public string Id { get; set; }

    [XmlElement("StartTimes")]
    public Collection<StartTime> StartTimeCollection;        
}

public class StartTime
{
    [XmlAttribute("Time")]
    public string Start { get; set; }
    [XmlAttribute("Mon")]
    public bool Monday { get; set; }
    [XmlAttribute("Tue")]
    public bool Tuesday { get; set; }
    [XmlAttribute("Wed")]
    public bool Wednesday { get; set; }
    [XmlAttribute("Thu")]
    public bool Thursday { get; set; }
    [XmlAttribute("Fri")]
    public bool Friday { get; set; }
    [XmlAttribute("Sat")]
    public bool Saturday { get; set; }
    [XmlAttribute("Sun")]
    public bool Sunday { get; set; }
}
公共类事件
{
[XmlAttribute(“Id”)]
公共字符串Id{get;set;}
[XmlElement(“StartTimes”)]
公众收集开始收集;
}
公共类开始时间
{
[XmlAttribute(“时间”)]
公共字符串开始{get;set;}
[xmldattribute(“Mon”)]
公共布尔星期一{get;set;}
[XmlAttribute(“Tue”)]
公共布尔星期二{get;set;}
[XmlAttribute(“Wed”)]
公共布尔星期三{get;set;}
[xmldattribute(“Thu”)]
公共布尔星期四{get;set;}
[XmlAttribute(“Fri”)]
公共布尔星期五{get;set;}
[XmlAttribute(“Sat”)]
公共学校星期六{get;set;}
[XmlAttribute(“Sun”)]
公共布尔星期日{get;set;}
}
xml看起来像:

<Event Id="f7cfc3a5-5b1b-4941-8d7b-f8a4a71fa530">
  <StartTimes>
    <StartTime Time="19:00" Mon="false" Tue="false" Wed="false" Thu="false" Fri="true" Sat="false" Son="false"/>
  </StartTimes>
</Event>
from x in doc.Descendants("Event")
select new Event()
{
Id = x.Attribute("Id").Value,
StartTimeCollection = x.Descendants("StartTimes") ????????? <-- That's the tricky part for me
}

这就是我的linq声明的样子:

<Event Id="f7cfc3a5-5b1b-4941-8d7b-f8a4a71fa530">
  <StartTimes>
    <StartTime Time="19:00" Mon="false" Tue="false" Wed="false" Thu="false" Fri="true" Sat="false" Son="false"/>
  </StartTimes>
</Event>
from x in doc.Descendants("Event")
select new Event()
{
Id = x.Attribute("Id").Value,
StartTimeCollection = x.Descendants("StartTimes") ????????? <-- That's the tricky part for me
}
来自文档子体中的x(“事件”)
选择新事件()
{
Id=x.属性(“Id”).值,
StartTimeCollection=x.substands(“StartTimes”)???由于
集合
公开了一个采用
IList
的文件,您可以使用并写入:

from x in doc.Descendants("Event")
select new Event() {
    Id = x.Attribute("Id").Value,
    StartTimeCollection = new Collection<StartTime>(
        x.Descendants("StartTimes").SelectMany(
            startTimes => startTimes.Elements("StartTime").Select(
                startTime => new StartTime() {
                    Start = startTime.Attribute("Time").Value,
                    Monday = Boolean.Parse(startTime.Attribute("Mon").Value),
                    Tuesday = Boolean.Parse(startTime.Attribute("Tue").Value),
                    Wednesday = Boolean.Parse(startTime.Attribute("Wed").Value),
                    Thursday = Boolean.Parse(startTime.Attribute("Thu").Value),
                    Friday = Boolean.Parse(startTime.Attribute("Fri").Value),
                    Saturday = Boolean.Parse(startTime.Attribute("Sat").Value),
                    Sunday = Boolean.Parse(startTime.Attribute("Son").Value)
                })).ToList())
}
来自文档子体中的x(“事件”)
选择新事件(){
Id=x.属性(“Id”).值,
StartTimeCollection=新集合(
x、 后代(“StartTimes”)。选择Many(
startTimes=>startTimes.Elements(“StartTime”)。选择(
startTime=>new startTime(){
Start=startTime.Attribute(“Time”).Value,
Monday=Boolean.Parse(startTime.Attribute(“Mon”).Value),
星期二=Boolean.Parse(startTime.Attribute(“Tue”).Value),
星期三=Boolean.Parse(startTime.Attribute(“Wed”).Value),
星期四=Boolean.Parse(startTime.Attribute(“Thu”).Value),
Friday=Boolean.Parse(startTime.Attribute(“Fri”).Value),
星期六=布尔.Parse(startTime.Attribute(“Sat”).Value),
Sunday=Boolean.Parse(startTime.Attribute(“Son”).Value)
})).ToList())
}

请注意,我使用了
Attribute(“Son”)
而不是
Attribute(“Sun”)
来初始化
Sunday
属性,因为该属性的名称与标记中的名称类似。不过,它可能是一个拼写错误。

太棒了!非常感谢!