Groups | Blog | Home
all groups > dotnet xml > may 2005 >

dotnet xml : .NET equivalent to XSLT value-of select


Martin Honnen
5/6/2005 12:00:00 AM


[quoted text, click to view]


[quoted text, click to view]

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/frlrfSystemXmlXPathXPathNavigatorClassEvaluateTopic.asp>


--

Martin Honnen --- MVP XML
Trillium
5/6/2005 7:47:31 AM
This seems like it should be really easy, but I cannot seem to make it work.

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.

Trillium
5/6/2005 8:58:02 AM
THANK YOU.
I had tried the Evaluate method, but was missing the "string()" part of the
expression. Now it works perfectly!

[quoted text, click to view]
AddThis Social Bookmark Button