tags:

views:

176

answers:

1

Hello,

I have some problem with my code

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;
}

I get this error:

Value cannot be null.
Parameter name: element 
Line 63: where (Guid)f.Element("id") == Id

The xml file:

<Page id="235487c9-f706-4550-831e-cc504e99d3c5">
  <Title>Test</Title>
  <Content>Test</Content>
  <PublishDate>Test</PublishDate>
  <Url>about/contact</Url>
</Page>
+1  A: 

You've asked for the id element, but you want the attribute:

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();

Any reason for returning it as an IQueryable<T>, by the way?

Jon Skeet
Ops, wrong by me. Thanks :)
Frozzare