【问题标题】:Why can't I select a single element in LINQ-To-XML?为什么我不能在 LINQ-To-XML 中选择单个元素?
【发布时间】:2010-01-06 20:57:29
【问题描述】:

在我的 XML 文档中选择单个元素的值时遇到了麻烦

我的文档看起来像

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

这些都不起作用。我不断收到:

对象引用未设置为 对象的实例。说明:一个 期间发生未处理的异常 当前网络的执行 要求。请查看堆栈跟踪 有关错误的更多信息 以及它起源于代码的位置。

异常详情: System.NullReferenceException:对象 引用未设置为 对象。

来源错误:

第 33 行:
x => x.Type);第 34 行:第 35 行:
AttachmentsPath = (从 e 到 mySettings.Descendants("设置") 第 36 行:
选择 e.Element("AttachmentsPath")).SingleOrDefault().Value; 第 37 行:

我可以看到它正确地加载到了 XML 文档中。

我在尝试访问我的 xml 文档中的这个单一设置值时做错了什么?哪种方式是正确的方式?

【问题讨论】:

    标签: c# .net xml linq-to-xml


    【解决方案1】:

    由于“MySettings”是根节点,它没有名为“MySettings”的后代

    试试

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

    但是,如果没有节点,SingleOrDefault 会返回 null,也许你可以试试这个更安全

    var AttachmentsPathElement = (from e in mySettings.Descendants("AttachmentsPath")
                                   select e).SingleOrDefault();
    
                if(AttachmentsPathElement != null)
                {
                    AttachmentsPath = AttachmentsPathElement.Value;
                }
    

    【讨论】:

      【解决方案2】:

      这行得通。

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

      【讨论】:

        【解决方案3】:

        这是不正确的代码,类的默认值是null,如果返回默认值你会得到一个空引用异常。

        SingleOrDefault().Value
        

        其次,如果您的第二种方法不起作用,这很可能意味着您无法正确加载 XML 文件,或者在 XML 中找不到元素“AttachmentsPath”。

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

        【讨论】:

        • 它不应该返回 null。鉴于我刚刚在上面给你的 XML。
        • @Godel,这不是重点。在调用 SingleOrDefault() 之后,您不应尝试访问任何属性或方法,而无需先检查其是否为空。在任何不好的代码中。
        • 如果它应该总是返回一些东西(从不为空),最好使用 Single()
        【解决方案4】:

        您就快到了,您所要做的就是指定 AttachmentPath 所在的根元素。

        就是这样……

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

        【讨论】:

          【解决方案5】:

          几个小时前我正在处理这样的事情。原来我要搜索的元素不存在。您的文档中是否可能没有名为“设置”的元素?

          【讨论】:

            【解决方案6】:

            为什么要在 XElement 中加载文档?为什么不是 XDocument?

            你可以试试这个:

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

            【讨论】:

              【解决方案7】:

              请尝试这种方式。我测试了这段代码,它工作正常

                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;
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2023-03-06
                • 1970-01-01
                • 1970-01-01
                • 2011-07-04
                • 1970-01-01
                相关资源
                最近更新 更多