C# 检查Linq to XML中是否存在节点

C# 检查Linq to XML中是否存在节点,c#,linq-to-xml,C#,Linq To Xml,我在一个文件夹中循环浏览一系列XML文件。它们几乎总是一样的,但我不时会遇到一个不包含其他元素的文件。例如,在一个文件中,它将如下所示: <sb_sbgp> <itemtitle>French-Canadian</itemtitle> <itemtype>subscription</itemtype> <audience>French people</audience> </sb_sbgp>

我在一个文件夹中循环浏览一系列XML文件。它们几乎总是一样的,但我不时会遇到一个不包含其他元素的文件。例如,在一个文件中,它将如下所示:

<sb_sbgp>
  <itemtitle>French-Canadian</itemtitle>
  <itemtype>subscription</itemtype>
  <audience>French people</audience>
</sb_sbgp>
<sb_sbgp>
  <itemtitle>Spanish</itemtitle>
  <itemtype>subscription</itemtype>
</sb_sbgp>
public static string ToNullIfEmpty(this string text)
{
    return string.IsNullOrEmpty(text) ? null : text;
}
我意识到这是为了测试是否有一个值,而不是一个元素,但我找不到任何东西可以让我测试元素的存在。帮助?

试试

XDocument doc = XDocument.Load("");
var x = (from node in doc.Descendants("sb_sbgp")
     select new
     {
         title = node.Element("itemtitle").Value,
         type = node.Element("itemtype").Value,
         audience = (node.Element("sb_sbgp_audience") != null) ? node.Element("sb_sbgp_audience").Value : null
     });
试试看


你很幸运-这其实很容易做到:

与使用.Value不同,just和LINQ to XML将执行您想要的操作:

audience = (string) node.Element("sb_sbgp_audience")
如果将null XElement或XAttribute引用强制转换为任何可为null的类型,无论是引用类型还是可为null的值类型,都将得到null结果。非常方便

唯一的缺点是,如果有空元素,仍然会得到空字符串而不是空引用。如果您真的想避免这种情况,我建议您创建一种如下的扩展方法:

<sb_sbgp>
  <itemtitle>French-Canadian</itemtitle>
  <itemtype>subscription</itemtype>
  <audience>French people</audience>
</sb_sbgp>
<sb_sbgp>
  <itemtitle>Spanish</itemtitle>
  <itemtype>subscription</itemtype>
</sb_sbgp>
public static string ToNullIfEmpty(this string text)
{
    return string.IsNullOrEmpty(text) ? null : text;
}
然后您可以使用:

audience = ((string) node.Element("sb_sbgp_audience")).ToNullIfEmpty()

你很幸运-这其实很容易做到:

与使用.Value不同,just和LINQ to XML将执行您想要的操作:

audience = (string) node.Element("sb_sbgp_audience")
如果将null XElement或XAttribute引用强制转换为任何可为null的类型,无论是引用类型还是可为null的值类型,都将得到null结果。非常方便

唯一的缺点是,如果有空元素,仍然会得到空字符串而不是空引用。如果您真的想避免这种情况,我建议您创建一种如下的扩展方法:

<sb_sbgp>
  <itemtitle>French-Canadian</itemtitle>
  <itemtype>subscription</itemtype>
  <audience>French people</audience>
</sb_sbgp>
<sb_sbgp>
  <itemtitle>Spanish</itemtitle>
  <itemtype>subscription</itemtype>
</sb_sbgp>
public static string ToNullIfEmpty(this string text)
{
    return string.IsNullOrEmpty(text) ? null : text;
}
然后您可以使用:

audience = ((string) node.Element("sb_sbgp_audience")).ToNullIfEmpty()
这个怎么样

XDocument doc = XDocument.Load(mydoc.xml);
var x = (from node in doc.Descendants("sb_sbgp")
         select new
         {
             title = node.Element("itemtitle").Value,
             type = node.Element("itemtype").Value,
             audience = node.Element("audience") != null) ? node.Element("audience").Value : null
         });
这个怎么样

XDocument doc = XDocument.Load(mydoc.xml);
var x = (from node in doc.Descendants("sb_sbgp")
         select new
         {
             title = node.Element("itemtitle").Value,
             type = node.Element("itemtype").Value,
             audience = node.Element("audience") != null) ? node.Element("audience").Value : null
         });
这个怎么样:

audience = node.Elements("sb_sbgp_audience").Count() > 0 ? node.Element("sb_sbgp_audience").Value : null
这个怎么样:

audience = node.Elements("sb_sbgp_audience").Count() > 0 ? node.Element("sb_sbgp_audience").Value : null

阿格!现在看到它,它似乎很简单!谢谢,谢谢。Jon,你的回答也很好,只是Saad看起来更像我想写的东西,如果这有任何意义的话。谢谢你们两位!阿格!现在看到它,它似乎很简单!谢谢,谢谢。Jon,你的回答也很好,只是Saad看起来更像我想写的东西,如果这有任何意义的话。谢谢你们两位!