- XML code
<class> <student> <no>1</no> <name>Ann</name> <sex>female</sex> <age>15</age> <score>90</score> </student> <student> <no>2</no> <name>Bob</name> <sex>male</sex> <age>16</age> <score>80</score> </student> <student> …… </student> <student> …… </student></class>
想获得score>90分学生信息(只要no、name、score),xpath应该怎么写啊?
------解决方案--------------------------------------------------------
- C# code
string strXml = @"<class> <student> <no>1</no> <name>Ann</name> <sex>female</sex> <age>15</age> <score>90</score> </student> <student> <no>2</no> <name>Bob</name> <sex>male</sex> <age>16</age> <score>80</score> </student> <student> <no>3</no> <name>ttt</name> <sex>j</sex> <age>15</age> <score>92</score> </student> <student> </student></class>"; XmlDocument xdoc = new XmlDocument(); xdoc.LoadXml(strXml); string strXPath = string.Empty; strXPath = "/class/student[score>=90]"; XmlNodeList oXmlNodes = xdoc.SelectNodes(strXPath); foreach (XmlNode node in oXmlNodes) { string strName = node.SelectSingleNode("/class/student/name").InnerText ; }
------解决方案--------------------------------------------------------
应该是不可以的。就像冰箱里有N多瓶饮料,你能否不开冰箱就喝到其中的一瓶呢。