How to find the max attribute from an XML document using Xpath 1.0

后端 未结 7 1060
无人及你
无人及你 2020-11-29 10:43

Is there a way to query an XML document to return the maximum of a given attribute using Xpath 1.0 ?

For example is there a way to get the max id ?

&         


        
7条回答
  •  醉酒成梦
    2020-11-29 10:52

    This example can be used to find the max.

    XmlDocument doc = new XmlDocument();                    
    doc.Load("../../Employees.xml");
    XmlNode node = doc.SelectSingleNode("//Employees/Employee/@Id[not(. <=../preceding-sibling::Employee/@id) and not(. <=../following-sibling::Employee/@Id)]");
    int maxId = Convert.ToInt32(node.Value);
    

    For other similar topics on xpath and linq check out http://rmanimaran.wordpress.com/2011/03/20/xml-find-max-and-min-value-in-a-attribute-using-xpath-and-linq/

提交回复
热议问题