Return list of Nodes from xpath expression from code
Hi,
I need to retrieve a list of nodes in a custom section I am writing (in VB.Net) but cannot see an easy way of doing this.
Library.GetXmlNodeByXPath only seems to return a single node, and umbraco.content.Instance.XmlContent is an issue due to it being ambiguous when used in VB (as discussed here)
I didn't ideally want to depend on installing an additional package if possible, but I have looked at the source of that uQuery method before for inspiration, but it uses the umbraco.content.Instance.XmlContent method, which unfortunately in VB you causes the ambiguous issue.
Sorry about that, if your XPath will work with a flat xml structure can I offer the following ?
public static List<Node> GetNodesByXPathViaSql(string xPath) { var nodes = new List<Node>(); var xmlDocument = uQuery.GetPublishedXml(UmbracoObjectType.Document); var xPathNavigator = xmlDocument.CreateNavigator(); var xPathNodeIterator = xPathNavigator.Select(xPath);
while (xPathNodeIterator.MoveNext()) { var node = uQuery.GetNode(xPathNodeIterator.Current.Evaluate("string(@id)").ToString()); if (node != null) { nodes.Add(node); } }
return nodes; }
The reason that the xml is flat is that it queries the cmlContentXml table and returns all xml fragments for every node, and simply joins them together - but I reckon this still could be quicker than navigating the object model.
Return list of Nodes from xpath expression from code
Hi,
I need to retrieve a list of nodes in a custom section I am writing (in VB.Net) but cannot see an easy way of doing this.
Library.GetXmlNodeByXPath only seems to return a single node, and umbraco.content.Instance.XmlContent is an issue due to it being ambiguous when used in VB (as discussed here)
cheers,
SImon
Hi Simon,
Have you seen the GetNodesByXPath method in uQuery ? This should do what you want, and returns a List<Node>.
HTH,
Hendy
Thanks Hendy,
I didn't ideally want to depend on installing an additional package if possible, but I have looked at the source of that uQuery method before for inspiration, but it uses the umbraco.content.Instance.XmlContent method, which unfortunately in VB you causes the ambiguous issue.
Hi Simon,
Sorry about that, if your XPath will work with a flat xml structure can I offer the following ?
The reason that the xml is flat is that it queries the cmlContentXml table and returns all xml fragments for every node, and simply joins them together - but I reckon this still could be quicker than navigating the object model.
HTH,
Hendy
Hi Hendy,
the only issue would be that I would have to install a 3rd party package to get the uQuery library, which is what I was trying to avoid.
Its suprised me how tough this is turning out to be, bearing in mind that Umbraco node structure is xml based and thats one of its great strengths.
Simon
is working on a reply...