Linq Query on XML file. Please, what am I doing wrong?

S

shapper

Hello,

I have the following query:

IQueryable<Bag> bags = (from b in context.Elements("Bags")
orderby b.Element("Updated").Value
descending
select new Bag {
Id = new Guid(b.Element("Id").Value),
Content = b.Element("Content").Value,
Created = DateTime.Parse(b.Element
("Created").Value),
Name = b.Element("Name").Value,
Open = Boolean.Parse(b.Element
("Open").Value),
Updated = DateTime.Parse(b.Element
("Updated").Value)
}).AsQueryable();
return bags;

I keep having the following error:
System.NullReferenceException: Object reference not set to an instance
of an object

On the following code line:
orderby b.Element("Updated").Value descending

I also get the same error if I add the following_
where b.Element("Name").Value == name

My XML file is as follows:
<?xml version="1.0" encoding="utf-8"?>
<Bags>
<Bag>
<Id>44334552-f7bc-4697-a5c8-e392a0ceff8b</Id>
<Content>sdsdf</Content>
<Created>13-06-2009 21:00:35</Created>
<Name>sdffsaf</Name>
<Open>True</Open>
<Updated>13-06-2009 21:00:35</Updated>
</Bag>
<Bag>
<Id>e82d6fed-0982-4260-a955-203525c4bad5</Id>
<Content>sadfdsf</Content>
<Created>13-06-2009 21:02:03</Created>
<Name>sdf</Name>
<Open>True</Open>
<Updated>13-06-2009 21:02:03</Updated>
</Bag>
</Bags>

What am I doing wrong?

Thanks,
Miguel
 
M

Martin Honnen

shapper said:
I have the following query:

IQueryable<Bag> bags = (from b in context.Elements("Bags")
orderby b.Element("Updated").Value
descending

I keep having the following error:
System.NullReferenceException: Object reference not set to an instance
of an object

On the following code line:
orderby b.Element("Updated").Value descending

I also get the same error if I add the following_
where b.Element("Name").Value == name

My XML file is as follows:
<?xml version="1.0" encoding="utf-8"?>
<Bags>
<Bag>
<Id>44334552-f7bc-4697-a5c8-e392a0ceff8b</Id>
<Content>sdsdf</Content>
<Created>13-06-2009 21:00:35</Created>
<Name>sdffsaf</Name>
<Open>True</Open>
<Updated>13-06-2009 21:00:35</Updated>
</Bag>
<Bag>
<Id>e82d6fed-0982-4260-a955-203525c4bad5</Id>
<Content>sadfdsf</Content>
<Created>13-06-2009 21:02:03</Created>
<Name>sdf</Name>
<Open>True</Open>
<Updated>13-06-2009 21:02:03</Updated>
</Bag>
</Bags>

What am I doing wrong?

You probably want

from b in context.Descendants("Bag")

or maybe

from b in context.Root.Elements("Bag")

as the "Bag" elements are those having an "Updated" or "Name" child element.
 
S

shapper

You probably want

   from b in context.Descendants("Bag")

or maybe

   from b in context.Root.Elements("Bag")

as the "Bag" elements are those having an "Updated" or "Name" child element.

Hi,

Yes I was able to solve it using "from b in context.Elements
("Bag").Elements("Bag")" but I followed your suggestion and I am using
"from b in context.Root.Elements("Bag")"

Thank You,
Miguel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top