【问题标题】:C# check an element exists while using LINQ to XMLC# 在使用 LINQ to XML 时检查元素是否存在
【发布时间】:2010-04-13 14:14:02
【问题描述】:

好的,有点随机的问题,但最好的方法是添加代码,您将能够立即明白我的意思:

XML:

<?xml version="1.0" encoding="utf-8" ?>
<customers>
  <customer>
    <id>1</id>
    <name>Blah-face</name>
    <Type>1</Type>
  </customer>
  <customer>
    <id>2</id>
    <name>Blah-face-2</name>
    <Type>2</Type>
  </customer>
  <customer>
    <id>3</id>
    <name>Blah-face-3</name>
    <Type>1</Type>
    <SuperType>1</SuperType>
  </customer>
</customers>

C#:

XDocument linquee = XDocument.Load(path);

var superType = (from c in linquee.Descendants("customer")
                 where (c.Element("SuperType").Value == "1")
                 select c).ToList();

这会出现一个空错误 - 我是否需要在每个客户之前添加一个带有空值的“SuperType”元素,或者是否有一种解决方法意味着我不必这样做?

干杯!

【问题讨论】:

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


    【解决方案1】:

    试试这个:

    var superType = (from c in from c in linquee.Descendants("customer")
                     where (string) c.Element("SuperType") == "1"
                     select c).ToList();
    

    基本上,如果您将一个空的XElement 引用转换为string,您将获得一个空引用(您可以将其与“1”进行比较)。

    另一种方法是强制转换为int?,如果元素丢失,(IIRC) 将返回 null int? 值,但如果它存在但非数字,则返回:

    var superType = (from c in from c in linquee.Descendants("customer")
                     where (int?) c.Element("SuperType") == 1
                     select c).ToList();
    

    【讨论】:

    • 完美,比检查空值更简单。一会儿就会“打勾”。
    【解决方案2】:

    你应该可以只添加一个空检查

    where c.Element("SuperType") != null 
    && [your other criteria]
    

    【讨论】:

      【解决方案3】:

      在尝试从中读取值之前,您是否尝试过检查 SuperType 元素是否存在?

      ...
      where (c.Element("SuperType") != null && c.Element("SuperType").Value == "1")
      ...
      

      【讨论】:

        【解决方案4】:

        我会这样做:

        var superType = linquee.Descendants("customer").
            Where(c => c.Element("SuperType") != null 
                && c.Element("SuperType").Value == "1");
        

        【讨论】:

          【解决方案5】:

          还应该能够通过扩展来清理这种东西,比如......

          public string Element_valStr(XElement xElm, string xName)
          {
              if (xElm.Element(xName) == null) return string.empty;
              return xElm.Element(xName).Value;
          }
          

          然后只是:

          var superType = (from c in linquee.Descendants("customer")  
                            where (c.Element_valStr("SuperType") == "1")
                            select c).ToList();
          

          【讨论】:

            【解决方案6】:

            我发现了一个使用 Any() 结合条件运算符的不错的解决方案:

            result = entry.Elements(ArbitraryElement).Any() ? (entry.Element(ArbitraryElement).Attributes(ArbitraryAttribute).Any() ? entry.Element(ArbitraryElement).Attribute(ArbitraryAttribute).Value : "-1") : "-1"

            诀窍是使用 Elements() 和 Any() 来检查元素是否存在(与 Attributes() 相同)

            所以对于这个例子,它会是这样的:

            var superType = from c in linquee.Descendants("customer")  
                            select c.Elements("SuperType").Any() ? c.Element("SuperType").Value : "0";
            

            【讨论】:

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