这是我的问题:
我有这个 XML 文件:
<?xml version="1.0" encoding="utf-8" ?>
<settings>
<app name="Application1">
<log name="Log1" path="d:\paths\" filename="Log1File"/>
<log name="Log2" path="d:\paths\"/>
<log name="log3" path="d:\paths\" filename="Log3File"/>
</app>
</settings>
我正在尝试使用 LINQ 阅读它并创建此类的对象:
public class Apps
{
public string Name { get; set; }
public IEnumerable<Logs> Logs { get; set; }
}
public class Logs
{
public string Name { get; set; }
public string Path { get; set; }
public string Filename { get; set; }
}
到目前为止,我设法创建了这段代码,但看起来它只获取第一个日志元素,平均时间我需要每个应用程序元素的所有日志元素:
public static IEnumerable<Apps> GetAllApps()
{
var items = from a in db.Descendants("app")
orderby a.Attribute("name").Value
select new Apps
{
Name = a.Attribute("name").Value,
Logs = from b in a.Descendants("log")
select new Logs
{
Name = b.Attribute("name").Value,
Path = b.Attribute("path").Value,
Filename = b.Attribute("filename").Value
}
};
return items;
}