grabbing a variable from an XML file

D

Darrel

I have a control that transforms an XML file using XSLT.

I also want to grab a particular variable out of the XML file. I thought an
easy way to do this would be no have the XSLT just find the variable, and
write it out as a hidden htmlInputHidden tag. I could then just grab the
value of that HTML tag via the codebehind.

This does not work, and, I assume, it's because I'm trying to both write out
the html AND grab a value in the same page_load...so it's a rather futile
method.

SO...that said, is the best way to just open a xml reader and grab it that
way? The more I type, the more I think I'm probably asking a dumb question
and asking the obvious here. ;o)

-Darrel
 
S

Scott Allen

Hi Darrel:

It sounds like the kind of scenario where the solution might be to
bring the XML into your own XPathNavigator instance. From there you
can find the data you need with something like:

XPathDocument doc = new XPathDocument("<xml filename>");
XPathNavigator navigator = doc.CreateNavigator();
XPathNodeIterator iterator = nav.Select("<xpath expression>");

and also bang it through a transformatio:

XslTransform xslt = new XslTransform();
xslt.Load("<xsl filename>");
// create a writer .. StringWriter HtmlTextWriter...
xslt.Transform(nav, null, writer, null);
 
D

Darrel

It sounds like the kind of scenario where the solution might be to
bring the XML into your own XPathNavigator instance. From there you
can find the data you need with something like:

I've actually just started down that path
XPathDocument doc = new XPathDocument("<xml filename>");
XPathNavigator navigator = doc.CreateNavigator();
XPathNodeIterator iterator = nav.Select("<xpath expression>";

I've gotten that far, but hit a snag:

Dim xpd As XPathDocument = New XPathDocument(filename)
'create the associated navigator
Dim xpn As XPathNavigator = xpd.CreateNavigator()
xpn.Select("//template(parent(descendant-or-self::id = '" & pageID & "')")

Here's what I'm trying to do:

The XML is the site navigation. It's hierarchical, obviously. I pass a
'pageID' variable to the page that I then use in the XML transformation to
find out the 'active' link and open up the nodes accordingly.

Another bit of information stored the xml file is the specific template
variable I am using for each page. This template is inherited from the
'parent' node. So, the structure is a bit like this:

items
item
id = 1
template = purple
item
id = 2
item
id = 3

etc.


So, I'd like to be able to open the xml file and find the root item's
template value that belongs to the node with the current active item who's
id = the current value of the page.

(I hope that makes sense ;o)

I *think* my statement needs to be something like this:

//template(parent(descendant-or-self::id = '$currentID'))

Does that look about right? I'm in a bit of a hurry to get out the door, so
my syntax above might be incorrect. That said, the problem is that I'm not
sure how to pass the 'currentID' to the document using an xmlnavigator. I
can do it with the transform, but can I do it with the navigator?

-Darrel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top