tags:

views:

933

answers:

2

I've got a XmlNodeList which I need to have it in a format that I can then re-use it within a XSLT stylesheet by calling it from a C# extension method, can anyone help? I have read that it might have something to do with using a XPathNavigator but I'm still a bit stuck.

Thanks in advanced,

Chris

+3  A: 

I had to solve this issue myself a couple of years ago. The only way I managed it was to create an XML fragment containing the nodes in the node list and then passing in the children of the fragment.

XsltArgumentList arguments = new XsltArgumentList();
XmlNodeList nodelist;
XmlDocument nodesFrament = new XmlDocument();
XmlNode root = nodesFragment.CreateElement("root");
foreach (XmlNode node in nodeList)
{
    root.AppendChild(node);
}
nodesFragment.AppendChild(root);

arguments.AddParam("argumentname", string.Empty, nodesFragment.CreateNavigator().SelectChildren(XPathNodeType.All));

Then you need to make sure you have the corresponding argument in your XSLT, of course.

Note that you probably don't need the additional XmlDocument. You could just call CreateNavigator() on the root XmlNode instance and use your existing XmlDocument for creating the element (I wrote this code some time ago and I've learned more since then - but I know the code above works, I haven't tried any alternatives).

Jeff Yates
A: 

The note at the end was the most useful, I had infact transformed the XmlNodeList into a XmlDocument already so could just use the Navigator on there and create it as a XPathNodeIterator.

Thanks for you help!

chrisntr
You are more than welcome. I remember how frustrated I got when I was trying to work this out.
Jeff Yates