-1

I have a code like:

jobdetails.group = TM_item.Group_xml.ToString(); //XML
var xDoc = XDocument.Parse(jobdetails.group);
var data = xDoc.Root.Elements().OrderBy(x => (string)x.Attribute("name"));

XML:

<Groups>
      <Group name="Front0">
        <Room_type>Front</Room_type>
        <Dimension>Not available</Dimension>
        <Status>PENDING</Status>
        <Notes>None</Notes>
        <User>r2g</User>
        <Audio_length>00:00:00</Audio_length>
        <Image_count>1</Image_count>
        <Section_count>0</Section_count>
      </Group>
</Groups>

I want to put where condition in xDoc.Root.Elements(),

I tried xDoc.Root.Elements().OrderBy(x => (string)x.Attribute("name")).Where(x => (string)x.Attribute("User").Value == loggedin_user); but it doesnot give me output..I am getting Object reference not set to an instance of an object. any suggestion?

Erik Philips
  • 48,663
  • 7
  • 112
  • 142
Dhara
  • 1,734
  • 1
  • 13
  • 33
  • 1
    possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – HimBromBeere Jul 30 '15 at 08:35
  • @HimBromBeere I just want to know whether my code is in correct form or not..Dont need of exception suggestion..thnx :) – Dhara Jul 30 '15 at 08:37
  • Well, it´s quite hard to guess where that exception comes from. From the pure syntax I guess your code should do its work. – HimBromBeere Jul 30 '15 at 08:39
  • you should add a check if attribute "User" exist before check the value of it `Where(x => x.Attribute("User") != null && x.Attribute("User").Value == loggedin_user)` – Maxime Porté Jul 30 '15 at 08:40
  • @MaximePorté I tried that also but getting same err – Dhara Jul 30 '15 at 08:48
  • Could you edit your post to add a sample of you xml code ? I would like to see what yours attribute looks like (is user / name on the same lever ? is user a sublever of name ? etc...) – Maxime Porté Jul 30 '15 at 08:53
  • @MaximePorté UPDATED with XML code – Dhara Jul 30 '15 at 08:56
  • 1
    User is not an attribute but a node ;) your problem looks like here – Maxime Porté Jul 30 '15 at 08:58
  • ooohh silly mistake..thnx alot (y) @MaximePorté – Dhara Jul 30 '15 at 09:00

2 Answers2

0

You are accessing Attribute("User") of Attribute("name"), then you are accessing user's property Value. One of these is null.

Try to construct a query which looks for elements with attribute name missing, or such that return an element with User attribute missing. I suppose you will find the guilty element.

Then reformat your query to protect, e.g. by adding Where(at => at != null) before accessing its properties.

Zoran Horvat
  • 9,447
  • 3
  • 26
  • 39
0

Use FirstOrDefault to get null when there is no result:

XElement value =  xdoc.Descendants()
    .Elements("Group")
    .Where(i => i.Attribute("name").Value == "Front0")
    .OrderBy(i => i.Attribute("name").Value)
    .FirstOrDefault();
thomasb
  • 5,209
  • 4
  • 57
  • 83
Learner
  • 71
  • 9