Solved

Check if XAttribute exists inside XElement

Posted on 2008-10-08
4
8,344 Views
Last Modified: 2013-12-17
Hey experts!

I am working on pulling ASP.NET MVC routes from an external file, and it's going well at the moment. My problem is that I want to be able to return the 'controller' attribute from the 'page' element, or the 'controller' attribute from the 'dir' element (parent of the 'page' element).

The problem is that when this runs, it causes an exception (ArgumentNullException) because I'm trying to retrieve the value of an attribute that doesn't exist.

I realise that, in the snippet, retrieving ControllerName is invalid, but I was trying to highlight what I want to  retrieve.

Any help would be great!

Thanks in advance!
XDocument xDoc = XDocument.Load("urls.xml");

var pages =

  from x in xDoc.Descendants().Elements("page")

  select new

  {

    Name = x.Parent.Attribute("name").Value,

    ControllerName = x.Attribute("controller").Value || x.Parent.Attribute("controller").Value,

    ControllerAction = x.Attribute("action").Value,

    Dir = x.Parent.Attribute("name").Value,

    Url = x.Parent.Attribute("name").Value + "/" + x.Attribute("url").Value,

  };

 

//An example of the urls.xml files is thus:

//

//<urls>

//  <dir name="Cheese" controller="Food"> <!-- Always a 'controller' attr on the 'dir' node -->

//    <page url="mexican.htm" action="mexican" controller="bar" />

//    <page url="cheddar.htm" action="cheddar" /> <!-- Note, no controller attribute here ('controller' attr is optional in 'page' node -->

//    <!--...-->

//  </dir>

//  <dir name="Alcohol" controller="Drink">

//  <!--...-->

//  </dir>

//</urls>

Open in new window

0
Comment
Question by:DanAtkinson
  • 3
4 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 22667753
If I am understanding you correctly and one of the values will at least by blank instead of null, you can use an inline if statement like this:
ControllerName = (x.Attribute("controller")==null ? x.Parent.Attribute("controller").Value : x.Attribute("controller").Value)

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22667768
Guess you could try nesting too.  Don't believe there are any restrictions stopping you from repeating an inline if in the true section of the first if for x.Parent.Attribute("controller") == null then set some default if true of x.Parent.Attribute("controller").Value if not.
0
 

Author Comment

by:DanAtkinson
ID: 22668101
Yep, that's the one! I was checking to see if the attribute value string was null or empty, rather than the attribute itself being null.

How stupid am I! :D

Thanks for your speedy answer!
//INVALID CODE

string.IsNullOrEmpty(x.Attribute("controller").Value) ? x.Parent.Attribute("controller").Value : x.Attribute("controller").Value

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22668961
You are welcome!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

937 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now