Trillium wrote:
I am trying to retrieve the text value of an element named "child2Element"
from an XML file in a .NET (v 1.1) with an XPath expression. In an XSLT
document I would use <xsl:value-of
select="rootElement/child1Element[@childId='110']/child2Element"/> (and this
does work fine in a transform). But I cannot seem to find the right method
or object in .NET. There seem to be a number of classes that can use XPath,
but I can't figure out which one I have to use to get just the text value of
a single element.
With XSLT 1.0 <xsl:value-of select="xpathexpression"> gives you the
string value of the Xpath expression, within .NET you can use an
XPathNavigator and its method Evaluate where you would need to explictly
call the XPath string function on your expression e.g.
using System;
using System.Xml;
using System.Xml.XPath;
public class Test2005050603 {
public static void Main (string[] args) {
// args[0] is the XML file URL,
// args[1] the XPath expression
XPathDocument xPathDocument = new XPathDocument(args[0]);
XPathNavigator xPathNavigator = xPathDocument.CreateNavigator();
string valueOf = (string) xPathNavigator.Evaluate("string(" + args2
+ ")");
Console.WriteLine(valueOf);
}
}
The method Evaluate is documented here:
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemXmlXPathXPathNavigatorClassEvaluateTopi c.asp>
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/