Re: .NET equivalent to XSLT value-of select

Tech-Archive recommends: Fix windows errors by optimizing your registry





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


--

	Martin Honnen --- MVP XML
	http://JavaScript.FAQTs.com/
.



Relevant Pages

  • Re: text/string finder
    ... > that returns a particular string I'm looking for? ... This is not a syntactically correct XPath expression. ... containing the string 'foo', ... Dimitre Novatchev [XML MVP], ...
    (microsoft.public.dotnet.xml)
  • Re: text/string finder
    ... >> I'm new to xml and sometimes gives me a hardtime. ... >> that returns a particular string I'm looking for? ... > This is not a syntactically correct XPath expression. ... > containing the string 'foo', ...
    (microsoft.public.dotnet.xml)
  • Re: How to combine data?
    ... Some processors provide an evaluate extension that allows you to ... evaluate a string as an XPath expression. ... George Cristian Bina - http://aboutxml.blogspot.com/ ...
    (microsoft.public.dotnet.xml)
  • Re: Xpath in expression shapes
    ... xpath expression still does not assign to the string :-( ... Greg. ... Prev by Date: ...
    (microsoft.public.biztalk.general)
  • Re: .NET equivalent to XSLT value-of select
    ... There seem to be a number of classes that can use XPath, ... > XPathNavigator and its method Evaluate where you would need to explictly ... > call the XPath string function on your expression e.g. ... > public static void Main { ...
    (microsoft.public.dotnet.xml)