缺少节点会导致 null 异常崩溃 linq to xml

发布于 2024-08-19 05:48:25 字数 411 浏览 3 评论 0原文

我收到一个 null 异常,因为 MYTAG1 不存在。我知道这是因为 Element("MYTAG1") 为空并且在其上调用 Elements("MYTAG2") 不起作用。

我该如何处理这个问题以防止崩溃?

     var myItems = from myNode in Nodes.Element("MYTAG1").Elements("MYTAG2")
                           select new EPTableItem
                           {
                           //    Assign stuff here                            
                           };

I get a null exception on this because MYTAG1 doesn't exist. I understand that this is because Element("MYTAG1") is null and calling Elements("MYTAG2") on it wont work.

How do I deal with this to prevent the crash?

     var myItems = from myNode in Nodes.Element("MYTAG1").Elements("MYTAG2")
                           select new EPTableItem
                           {
                           //    Assign stuff here                            
                           };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

得不到的就毁灭 2024-08-26 05:48:25

我想不出一种巧妙的方法将 if 语句合并到 C# 查询语法中,因此我将提出以下解决方案,在执行查询之前检查所需的节点。

var myItems;
XElement myTag1 = myNode.Element("MYTAG1");

if (myTag1 != null)
{
    myItems = from myNode in myTag1.Elements("MYTAG2")  
              select new EPTableItem  
              {  
                  //    Assign stuff here                              
              };  
}

I can't think of a clever way to incorporate an if statement into the C# query syntax, so I'll propose the following solution which checks for the required node before executing the query.

var myItems;
XElement myTag1 = myNode.Element("MYTAG1");

if (myTag1 != null)
{
    myItems = from myNode in myTag1.Elements("MYTAG2")  
              select new EPTableItem  
              {  
                  //    Assign stuff here                              
              };  
}
·深蓝 2024-08-26 05:48:25

一种选择是定义一个新的扩展方法并将其用作查询源。

static IEnumerable<XElement> ElementAndChildren(this XElement parent, string name, string childName) 
{
    var element = parent.Element(name);
    if (element == null)
    {
        return Enumerable.Empty<XElement>();
    }
    return element.Elements(childName);
}

...
var myItems = from myNode in Nodes.ElementAndChildren("MYTAG1","MYTAG2")
                       select new EPTableItem
                       {
                       //    Assign stuff here                            
                       };

One option is to define a new extension method and use that as your query source.

static IEnumerable<XElement> ElementAndChildren(this XElement parent, string name, string childName) 
{
    var element = parent.Element(name);
    if (element == null)
    {
        return Enumerable.Empty<XElement>();
    }
    return element.Elements(childName);
}

...
var myItems = from myNode in Nodes.ElementAndChildren("MYTAG1","MYTAG2")
                       select new EPTableItem
                       {
                       //    Assign stuff here                            
                       };
命比纸薄 2024-08-26 05:48:25

我发现当您使用扩展方法而不是伪 sql 语法时,Linq 会容易得多。您应该能够按照这些思路做一些事情,但请记住我尚未测试代码。

var myItems = Nodes.Where(n => n.Element("MYTAG1") != null)
   .Select(n => n.Element("MYTAG1"))
   .Select(elem => elem.Elements("MYTAG2"))
   .Select(elem2 => new EPTTableItem { something = elem2.SomeProperty ... } );

I find that Linq is a lot easier when you use the extension methods instead of the pseudo sql syntax. You should be able to do something along these lines, but bare in mind that I have not tested the code.

var myItems = Nodes.Where(n => n.Element("MYTAG1") != null)
   .Select(n => n.Element("MYTAG1"))
   .Select(elem => elem.Elements("MYTAG2"))
   .Select(elem2 => new EPTTableItem { something = elem2.SomeProperty ... } );
偏爱自由 2024-08-26 05:48:25

如果你想在 LINQ 中执行此操作,类似这样的操作应该可以工作:

var myItems = from node in (from myNode in Nodes.Elements("MYTAG1")
              where myNode != null
              select myNode).SelectMany(x => x.Elements("MYTAG2"))
              select new EPTableItem
              {
                // Assign stuff here                            
              };

当然,如果 MYTAG 出现更多次,则工作方式会有所不同

If you want to do it in LINQ something like this should work:

var myItems = from node in (from myNode in Nodes.Elements("MYTAG1")
              where myNode != null
              select myNode).SelectMany(x => x.Elements("MYTAG2"))
              select new EPTableItem
              {
                // Assign stuff here                            
              };

of course this will work differently if there's more occurrences of MYTAG

时光是把杀猪刀 2024-08-26 05:48:25

您可以使用单独的 from 子句和 where 子句在单个 LINQ 查询中完成这一切。

var myItems = 
        from tag1 in Nodes.Elements("MYTAG1")
        where tag1 != null
        from tag2 in tag1.Elements("MYTAG2")
        select new EPTableItem
        {
        //    Assign stuff here                            
        };

You can do it all in a single LINQ query with separate from clauses and a where clause.

var myItems = 
        from tag1 in Nodes.Elements("MYTAG1")
        where tag1 != null
        from tag2 in tag1.Elements("MYTAG2")
        select new EPTableItem
        {
        //    Assign stuff here                            
        };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文