364,085 Members | 5289 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

.NET equivalent to XSLT value-of select

Trillium
P: n/a
Trillium
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.

Can someone point me in the right direction?
Nov 12 '05 #1
Share this Question
Share on Google+
2 Replies


Martin Honnen
P: n/a
Martin Honnen


Trillium wrote:

[color=blue]
> 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.[/color]

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/
Nov 12 '05 #2

Trillium
P: n/a
Trillium
THANK YOU.
I had tried the Evaluate method, but was missing the "string()" part of the
expression. Now it works perfectly!

"Martin Honnen" wrote:
[color=blue]
>
>
> Trillium wrote:
>
>[color=green]
> > 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.[/color]
>
> 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/
>[/color]
Nov 12 '05 #3

Post your reply

Help answer this question



Didn't find the answer to your .NET Framework question?

You can also browse similar questions: .NET Framework