【问题标题】:Get the Max Value of List Node From XML从 XML 中获取列表节点的最大值
【发布时间】:2014-04-19 11:21:40
【问题描述】:

我正在使用 Vb.net,我需要从以下 XML 获取访问的最大值。

<Pages>
<Page posted="2006-03-27" visits="148" title="Don't Sweep That Under the Rug!"/>
<Page posted="2006-07-12" visits="191" title="Tire Swings for Grownups"/>
<Page posted="2006-11-07" visits="214" title="Eliminate Hornets from Your Picnic"/>
<Page posted="2006-06-14" visits="296" title="Why Ants Invade Your Kitchen"/>
<Page posted="2006-01-15" visits="227" title="101 Ways to Climb a Tree"/>
<Page posted="2006-07-28" visits="133" title="The Beauty of a Frost-Free Refrigerator"/>
<Page posted="2006-03-31" visits="316" title="How to Achieve Restful Sleep"/>
<Page posted="2006-09-21" visits="232" title="Buying Your First Car"/>
</Pages>

我已尝试使用以下代码,它工作正常。

    Dim Node As XmlNode = XmlDocumnet.SelectSingleNode("/Pages/Page/@visits[not(. <= ../preceding-sibling::Page/@visits) and not(. <=../following-sibling::Page/@visits)]")

    If Node IsNot Nothing AndAlso Node.Value <> "" Then
        MaxVisit= Convert.ToInt32(Node.Value) + 1
    End If

但如果 Visist 属性具有重复值,则无法正确找到它。 即如果发现重复访问和空访问,则最大值不存在。

例如:

 <Page posted="2006-07-12" visits="214" title="Tire Swings for Grownups"/>
 <Page posted="2006-11-07" visits="214" title="Eliminate Hornets from Your Picnic"/>

或:

 <Page posted="2006-07-12" visits="" title="Tire Swings for Grownups"/>
 <Page posted="2006-11-07" visits="214" title="Eliminate Hornets from Your Picnic"/>

【问题讨论】:

标签: asp.net xml vb.net xpath max


【解决方案1】:

如果有多个具有最大 visits 的节点,要返回第一次出现而不是没有结果,请将 XPath 更改为使用 &lt; 而不是 &lt;=

/Pages/Page/@visits[
        not(. < ../preceding-sibling::Page/@visits) 
            and 
        not(. < ../following-sibling::Page/@visits)
        ]

更新:

除了上面的XPath,还要过滤掉空的visits,你可以试试这个方法:

/Pages/Page/@visits[
        not(. < ../preceding-sibling::Page/@visits) 
            and 
        not(. < ../following-sibling::Page/@visits)
            and
        normalize-space(.)
        ]

【讨论】:

  • 您好,感谢您的帮助。我已经尝试过你的解决方案。它正在工作,但如果我有 访问的空值visits="" 那么它只返回 0....
  • 您可以使用 XPath normalize-space() 函数过滤掉空值,例如:...[... and normalize-space(.)]
  • 您能否在回答中反映这一点。我是新手
【解决方案2】:

您可以使用以下 Linq to Xml 而不是使用 XPath 查询 XmlDocument:

Dim el = XDocument.Parse(xmlInput)
Dim maxVisits = el.Descendants("Page") _
                  .Select(Function(x) New With { _
                                      .Title = x.Attribute("Title").Value, _
                                      .Visits = If(String.IsNullPrEmpty(x.Attribute.Value), 
                                                   0, 
                                                   Integer.Parse(x.Attribute("Visits").Value)) }) _
                  .OrderByDescending(Function(x) x.Visits) _
                  .FirstOrDefault()

【讨论】:

  • 您好,感谢您的帮助。我已经尝试过你的解决方案。它正在工作,但如果我有 Visit 的空值visits="" 那么它只会返回错误,因为 输入字符串格式不正确
  • @VigneshKumar:我已经为字符串添加了针对 null 或空的检查。如果属性为空,它现在也应该可以工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多