I m 有一个假日,选择我XML文件中单一要素的价值
我的文件 like切。
<?xml version="1.0" encoding="utf-8" ?>
<MySettings>
<AttachmentsPath>Test</AttachmentsPath>
<PendingAttachmentsPath>Test2</PendingAttachmentsPath>
</MySettings>
我试图做以下工作:
XElement mySettings = XElement.Load("MySettings.xml");
string AttachmentsPath = (from e in mySettings.Descendants("MySettings")
select e.Element("AttachmentsPath")).SingleOrDefault().Value;
或
XElement mySettings = XElement.Load("MySettings.xml");
string AttachmentsPath = mySettings.Element("AttachmentsPath").Value;
And none of these w或k. 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 f或 m或e inf或mation about the err或 and where it 或iginated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Err或:
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 c或rectly.
在我xml文件中试图利用这一单一环境,我做了什么错误? 如何适当?