开发者

Why can't I select a single element in LINQ-To-XML?

开发者 https://www.devze.com 2022-12-15 20:25 出处:网络
I\'m having adevil of a time selecting the value of a single element in my XML document My document looks like

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>Te开发者_如何学Pythonst</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?


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;
            }


This works.

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


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;


You were almost there, all you have to do is specify the root element where the AttachmentPath resides.

That's it...

string attachmentsPath= mySettings.Root.Element("MySettings")
                .Elements("AttachmentsPath").SingleOrDefault().Value;


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?


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;


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消