English 中文(简体)
为什么我能选择准则Q-To-XML中的一个单一要素?
原标题:Why can t I select a single element in LINQ-To-XML?

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文件中试图利用这一单一环境,我做了什么错误? 如何适当?

问题回答

由于“MySettings”是它没有称为“MySettings”的后裔的深层 no。

审判

 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 审判 this as safer

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

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

这一工作。

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

这是一种不正确的代码,如果退款,就会有一个无效的参考例外。

SingleOrDefault().Value

第二,如果你的第二位办法不从事最可能意味着你无法正确装载XML文件的工作,或无法在XML中找到“AttachmentsPath”。

 XElement mySettings = XElement.Load("MySettings.xml");
 string AttachmentsPath = mySettings.Element("AttachmentsPath").Value;

你们几乎在那里,你们都必须做的是具体阐述阿特帕塔人居住的根本内容。

......

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

我在几个小时前刚刚处理过这样的问题。 事实是,我正在寻找的那部分。 您的文件中没有一个名为“定居”的内容吗?

为什么你把文件装上XElement? 为什么没有XDocument?

你可以尝试:

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

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

请尝试这样做。 我测试了该法典的操作标准。

  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;




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签