我的代码存在问题
public IQueryable<PageItems> GetPageById(Guid Id)
{
var xml = Utility.LoadXmlFile("Pages/" + Id);
var q = (from f in xml.Descendants("Page")
where (Guid)f.Element("id") == Id
select new PageItems
{
Title = f.Element("Title").Value,
Content = f.Element("Content").Value,
PublishDate = f.Element("PublishDate").Value,
}).AsQueryable();
return q;
}
我收到此错误:
Value cannot be null.
Parameter name: element
Line 63: where (Guid)f.Element("id") == Id
xml文件:
<Page id="235487c9-f706-4550-831e-cc504e99d3c5">
<Title>Test</Title>
<Content>Test</Content>
<PublishDate>Test</PublishDate>
<Url>about/contact</Url>
</Page>
答案 0 :(得分:1)
您已经要求提供id 元素,但您需要属性:
var q = (from f in xml.Descendants("Page")
where (Guid)f.Attribute("id") == Id
select new PageItems
{
Title = f.Element("Title").Value,
Content = f.Element("Content").Value,
PublishDate = f.Element("PublishDate").Value,
}).AsQueryable();
顺便说一下,将它作为IQueryable<T>
返回的原因是什么?