2009-09-05 6 views
1

У меня есть некоторые проблемы с моим кодомLinq для XML, где значение равно нулю

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> 

ответ

1

Вы» ve просил идентификатор элемент, но вы хотите, чтобы атрибут :

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>, кстати?

+0

Операции, неправильные мной. Благодаря :) – 2009-09-05 14:59:03

Смежные вопросы