views:

312

answers:

6

I'm having a devil of a time selecting the value of a single element in my XML document

My document looks like

<?xml version="1.0" encoding="utf-8" ?>
<MySettings>
  <AttachmentsPath>Test</AttachmentsPath>
  <PendingAttachmentsPath>Test2</PendingAttachmentsPath>
</MySettings>

I've tried to do the following:

 XElement mySettings = XElement.Load("MySettings.xml");

 string AttachmentsPath = (from e in mySettings.Descendants("MySettings")
                              select e.Element("AttachmentsPath")).SingleOrDefault().Value;

or

 XElement mySettings = XElement.Load("MySettings.xml");

     string AttachmentsPath = mySettings.Element("AttachmentsPath").Value;

And none of these work. I keep getting a:

Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 33:
x => x.Type); Line 34: Line 35:
AttachmentsPath = (from e in mySettings.Descendants("Settings") Line 36:
select e.Element("AttachmentsPath")).SingleOrDefault().Value; Line 37:

I can see that it loaded in the XML document correctly.

What am I doing wrong in trying to access this single settings value in my xml document? Which way is the proper way?

A: 

I was just dealing with something like this a couple hours ago. It turned out that the element I was searching for didn't exist. Is it possible you don't have an element named "Settings" in your document?

Randy Minder
+1  A: 

This is incorrect code, default of a class is null , if default is returned you will get a null reference exception.

SingleOrDefault().Value

Second of all, if your 2nd approach doesn't work that most likely means you cannot load the XML file correctly, or it cannot find the Element "AttachmentsPath" in the XML.

 XElement mySettings = XElement.Load("MySettings.xml");
 string AttachmentsPath = mySettings.Element("AttachmentsPath").Value;
Stan R.
it shouldn't be returning null. Given the XML I just gave you above.
Godel
@Godel, thats not the point. You shouldn't try to access any properties or methods after calling *SingleOrDefault()* without first checking if its null. In any code that is bad practice.
Stan R.
If it should always return something (never null), it's better to use Single()
borisCallens
+1  A: 

As "MySettings" is the root node it does not have a descendant called "MySettings"

try

 var AttachmentsPath = (from e in mySettings.Descendants("AttachmentsPath")
                               select e).SingleOrDefault().Value;

however as SingleOrDefault returns null if there is no node maybe you could try this as safer

var AttachmentsPathElement = (from e in mySettings.Descendants("AttachmentsPath")
                               select e).SingleOrDefault();

            if(AttachmentsPathElement != null)
            {
                AttachmentsPath = AttachmentsPathElement.Value;
            }
Pharabus
A: 

This works.

string path = mySettings.Element("AttachmentsPath").Value;
mdm20
A: 

Why are you loading a document in XElement? Why not XDocument?

You could try this:

XDocument mySettings = XDocument.Load("MySettings.xml");

string AttachmentsPath = mySettings.Root.Element("AttachmentsPath").Value;
Philippe Leybaert
A: 

Please try on this way.I tested this code its working fine

  XDocument xmlDoc = XDocument.Load(fileName);

    XElement page = xmlDoc.Descendants("MySettings").FirstOrDefault();

   string AttachmentsPath  =  page.Descendants("AttachmentsPath").First().Value;

   string PendingAttachmentsPath=  page.Descendants("PendingAttachmentsPath").First().Value;
Pankaj