我想传入一个xpath查询并返回我找到的值。 我正在寻找特定的属性值。如何使用XPath获取c#中属性的值?
_query = "//@RequestType";
我可以返回节点,但我不知道如何从中获取字符串值。 我想查询下面的xml的type
属性,并获得“xpath”。
这也将是很好,如果我可以只是替换我的查询,也从好的价值“是”回来。
<?xml version="1.0" ?>
<test type="xpath">
<nice>yes</nice>
</test>
C#
public string Locate(string message)
{
using (var stream = new MemoryStream(Encoding.GetBytes(message)))
{
var doc = new XPathDocument(stream);
var nav = doc.CreateNavigator();
var result = nav.Select(_query);
if (result != null)
{
return result
}
}
}