C# Silverlight C LINQ到XML
我正在开发一个Silverlight web应用程序,该应用程序使用XML,类似于:C# Silverlight C LINQ到XML,c#,linq,silverlight,C#,Linq,Silverlight,我正在开发一个Silverlight web应用程序,该应用程序使用XML,类似于: <?xml version="1.0" encoding="UTF-8" ?> <ProjectList> <Type>web</Type> <Project> <Id>1</Id> <Name>test web project</Name>
<?xml version="1.0" encoding="UTF-8" ?>
<ProjectList>
<Type>web</Type>
<Project>
<Id>1</Id>
<Name>test web project</Name>
<Description>test web project</Description>
<ScreenshotList>
<Screenshot>
<Path>screen1.jpg</Path>
<Description>This a description of screen 1</Description>
</Screenshot>
<Screenshot>
<Path>screen2.jpg</Path>
<Description>This a description of screen 2</Description>
</Screenshot>
<Thumb>noThumb.jpg</Thumb>
</ScreenshotList>
</Project>
</ProjectList>
在这个查询中,我是否可以轻松地获取屏幕截图并将其添加到Project实例的列表中
编辑-添加项目构造函数
public Project(int id, string name, string description, string thumbPath)
{
this.id = id;
this.name = name;
this.description = description;
this.thumbPath = thumbPath;
}
好吧,你还没有展示项目构造器的样子。。。它是否允许您以IEnumerable的形式传递屏幕截图?如果是这样,那应该很容易。。。比如:
public Project(int id, string name, string description, string thumbPath)
{
this.id = id;
this.name = name;
this.description = description;
this.thumbPath = thumbPath;
}
var projects =
from p in xDoc.Root.Elements("Project")
select new Project(Int32.Parse(project.Element("Id").Value,
CultureInfo.InvariantCulture),
p.Element("Name").Value,
p.Element("Description").Value,
p.Element("ScreenshotList")
.Element("Thumb").Value,
p.Element("ScreenshotList")
.Elements("Screenshot")
.Select(ss =>
new Screenshot(ss.Element("Path").Value,
ss.Element("Description").Value))
);
var projects = from project in xDoc.Root.Elements("Project")
let list = project.Element("ScreenshotList")
select new Project(
(int) project.Element("Id"),
(string)project.Element("Name"),
(string)project.Element("Description"),
(string)list.Element("Thumb"),
from scr in list.Elements("Screenshot")
select new Screenshot(
(string)scr.Element("Path"),
(string)scr.Element("Description")
)
);
为了避免滚动,我重新设置了一些格式。好吧,您还没有显示项目构造函数的外观。。。它是否允许您以IEnumerable的形式传递屏幕截图?如果是这样,那应该很容易。。。比如:
public Project(int id, string name, string description, string thumbPath)
{
this.id = id;
this.name = name;
this.description = description;
this.thumbPath = thumbPath;
}
var projects =
from p in xDoc.Root.Elements("Project")
select new Project(Int32.Parse(project.Element("Id").Value,
CultureInfo.InvariantCulture),
p.Element("Name").Value,
p.Element("Description").Value,
p.Element("ScreenshotList")
.Element("Thumb").Value,
p.Element("ScreenshotList")
.Elements("Screenshot")
.Select(ss =>
new Screenshot(ss.Element("Path").Value,
ss.Element("Description").Value))
);
var projects = from project in xDoc.Root.Elements("Project")
let list = project.Element("ScreenshotList")
select new Project(
(int) project.Element("Id"),
(string)project.Element("Name"),
(string)project.Element("Description"),
(string)list.Element("Thumb"),
from scr in list.Elements("Screenshot")
select new Screenshot(
(string)scr.Element("Path"),
(string)scr.Element("Description")
)
);
为了避免滚动,我重新设置了一些格式。类似于:
var projects =
from p in xDoc.Root.Elements("Project")
select new Project(Int32.Parse(project.Element("Id").Value,
CultureInfo.InvariantCulture),
p.Element("Name").Value,
p.Element("Description").Value,
p.Element("ScreenshotList")
.Element("Thumb").Value,
p.Element("ScreenshotList")
.Elements("Screenshot")
.Select(ss =>
new Screenshot(ss.Element("Path").Value,
ss.Element("Description").Value))
);
var projects = from project in xDoc.Root.Elements("Project")
let list = project.Element("ScreenshotList")
select new Project(
(int) project.Element("Id"),
(string)project.Element("Name"),
(string)project.Element("Description"),
(string)list.Element("Thumb"),
from scr in list.Elements("Screenshot")
select new Screenshot(
(string)scr.Element("Path"),
(string)scr.Element("Description")
)
);
基于以下类型:
class Project {
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Thumb { get; set; }
public List<Screenshot> Screenshots { get; private set; }
public Project( int id, string name, string description, string thumb,
IEnumerable<Screenshot> screenshots) {
this.Id = id;
this.Name = name;
this.Description = description;
this.Thumb = thumb;
this.Screenshots = screenshots == null ? new List<Screenshot>()
: new List<Screenshot>(screenshots);
}
}
class Screenshot {
public string Path { get; set; }
public string Description { get; set; }
public Screenshot(string path,string description) {
this.Path = path;
this.Description = description;
}
}
比如:
var projects =
from p in xDoc.Root.Elements("Project")
select new Project(Int32.Parse(project.Element("Id").Value,
CultureInfo.InvariantCulture),
p.Element("Name").Value,
p.Element("Description").Value,
p.Element("ScreenshotList")
.Element("Thumb").Value,
p.Element("ScreenshotList")
.Elements("Screenshot")
.Select(ss =>
new Screenshot(ss.Element("Path").Value,
ss.Element("Description").Value))
);
var projects = from project in xDoc.Root.Elements("Project")
let list = project.Element("ScreenshotList")
select new Project(
(int) project.Element("Id"),
(string)project.Element("Name"),
(string)project.Element("Description"),
(string)list.Element("Thumb"),
from scr in list.Elements("Screenshot")
select new Screenshot(
(string)scr.Element("Path"),
(string)scr.Element("Description")
)
);
基于以下类型:
class Project {
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Thumb { get; set; }
public List<Screenshot> Screenshots { get; private set; }
public Project( int id, string name, string description, string thumb,
IEnumerable<Screenshot> screenshots) {
this.Id = id;
this.Name = name;
this.Description = description;
this.Thumb = thumb;
this.Screenshots = screenshots == null ? new List<Screenshot>()
: new List<Screenshot>(screenshots);
}
}
class Screenshot {
public string Path { get; set; }
public string Description { get; set; }
public Screenshot(string path,string description) {
this.Path = path;
this.Description = description;
}
}
我添加了我当前的构造函数。我现在将尝试将它们作为参数传递给它。我添加了当前的构造函数。我现在将尝试将它们作为参数传递给它;DateTime-如果您强制转换,它将使用正确的xsd日期格式-是的。比较查询,使用强制转换比使用解析更容易阅读。请注意,您不需要所有的Int32.parse{expr}.Value-您只需强制转换XElement-更简单,并为丢失的元素提供了更清晰的行为,您可以强制转换为null,etcFor info-使用强制转换而不是解析的另一个原因;DateTime-如果您强制转换,它将使用正确的xsd日期格式-是的。比较这些查询,使用cast比使用parse更容易阅读。你的解决方案很有效,但我给了Jon Skeet一个标记,因为他首先到达了那里。感谢您再次查看,我更喜欢这个查询。你知道了!你的解决办法奏效了,但我给了乔恩·斯基特分数,因为他先到了。感谢您再次查看,我更喜欢这个查询。你知道了!