如何在XElement中使用XPath

在XElement中并没有对XPath的支持,需要通过扩展实现,扩展类为System.Xml.XPath.Extensions。所以通过引用System.Xml.XPath命名空间后即可使用XPath方式筛选元素。例子如下:

string xml = "<body><scoreLogSchema><item><logTime>20100326170319</logTime><score>100</score></item><item><logTime>20100325150315</logTime><score>999</score></item></scoreLogSchema></body>";

var elem = XElement.Parse(xml);

Response.Write(elem.XPathSelectElement("//scoreLogSchema").ToString());

原文地址:https://www.cnblogs.com/chenjunbiao/p/1760294.html