Solved

Check if XAttribute exists inside XElement

Posted on 2008-10-08
4
8,645 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 60

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 60

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 60

Expert Comment

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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

724 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