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?