473,322 Members | 1,690 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

.NET equivalent to XSLT value-of select

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
2 1254


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/
Nov 12 '05 #2
THANK YOU.
I had tried the Evaluate method, but was missing the "string()" part of the
expression. Now it works perfectly!

"Martin Honnen" wrote:


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/

Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Son KwonNam | last post by:
In XSLT, is this possible to get value from xml using XPath which is in XSLT variable? I mean XPath strings can be dynamic while XSL Transforming. If possible, How?? Because I'm not a...
6
by: chonkme | last post by:
Hi, i have a real simple xslt problem but i just cant figure out how to do it by looking at various examples on the net. i have a xml document and in it are some elements with a "result" tag name....
7
by: RC | last post by:
First, let me say I couldn't find a group discuss XML/XSLT. So I only choose the closest groups to post this message. Here is part of my *.xsl file <xsl:stylesheet...
6
by: RC | last post by:
Hello World, I am try do call a JavaScript function from XSLT, but I got function not avaible error. See "????" below. Would someone out there tell me how? Thank Q! <xsl:stylesheet...
5
by: Praveen Mohanan | last post by:
Hi...All, I am new to XSLT. I have a XML file which need to be converted to another XML file for the client. I am using a XSLT . File 1 ------ <client name="abc" address="..." city="..."...
10
by: daz_oldham | last post by:
Hi I am doing an XSLT that processes (for example) a list of hotels. Each hotel has the attribute @StarRating which is one of the following values: * ** *** ****
21
by: Damian | last post by:
Hi, I'm from an ASP.NET background an am considering making the switch to Python. I decided to develop my next project in tandem to test the waters and everything is working well, loving the...
0
by: poli | last post by:
Hi, It is my first xslt document that I have to write. And I have to do it quick (1 day). PLease help me. I have this input xml file: <?xml version="1.0" encoding="UTF-8"?> <Node...
0
by: ronc85 | last post by:
My environment is ASP.NET 2.0, C# and AJAX. I'm having a problem with 'Button Click' logic which is trying to display a Gridview and then use XML/XSLT to build an Excel file. The following code...
12
by: Stu | last post by:
Being a newbie with XSLT transformation code please excuse my neivte. In addition, I am not sure what I want to do can be done with xslt so I apologize up front for asking anything stupid I...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.