Asp.net 从XML中获取列表节点的最大值

Asp.net 从XML中获取列表节点的最大值,asp.net,xml,vb.net,xpath,max,Asp.net,Xml,Vb.net,Xpath,Max,我正在使用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="2

我正在使用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

Dim Node为XmlNode=XmlDocumnet。选择singlenode(“/Pages/Page/@visions[not(.)以返回第一个匹配项,而不是无结果。如果有多个节点具有最大的
访问次数,请将XPath更改为使用
,您可以使用以下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()

“未正确找到”是什么意思?如果发现重复访问,则最大值不存在。-webkit box shadow:0 0 10px 0px#110B0B inset;您好,谢谢您的帮助。我尝试了您的解决方案。它正在工作,但如果我的访问值为空,如
visions=”“
然后它只返回0….您可以使用XPath
normalize-space()
函数筛选空值,如:
…[…和normalize space(.)]
请您在回答中反映这一点。我是新手。谢谢您的帮助。我已经尝试了您的解决方案。它可以工作,但如果我的访问值为空,如
visions=“”
然后它返回错误,因为输入字符串的格式不正确。@VigneshKumar:我已经为字符串添加了null或empty检查。如果属性为空,它现在也应该可以工作。
 <Page posted="2006-07-12" visits="" title="Tire Swings for Grownups"/>
 <Page posted="2006-11-07" visits="214" title="Eliminate Hornets from Your Picnic"/>
/Pages/Page/@visits[
        not(. < ../preceding-sibling::Page/@visits) 
            and 
        not(. < ../following-sibling::Page/@visits)
        ]
/Pages/Page/@visits[
        not(. < ../preceding-sibling::Page/@visits) 
            and 
        not(. < ../following-sibling::Page/@visits)
            and
        normalize-space(.)
        ]
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()