![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#11
| |||
| |||
|
|
* David Thielen wrote in microsoft.public.dotnet.xml: The problem with that is a user can feed us anything for the xpath - including true(). What we really need is something that returns any of the following: 1) not a legit xpath statement 2) xpath to a node - node exists 3) xpath to a node - node does not exist 4) other - like name(/root/node) You can use the .Compile method which will return a XPathExpression object which has a returnType property of type XPathResultType which in turn will allow you to distinguish these cases. Evaluating the ex- pression will then tell you whether there are matching nodes. -- Björn Höhrmann · mailto:bjoern (AT) hoehrmann (DOT) de · http://bjoern.hoehrmann.de Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de 68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/ |
#12
| |||
| |||
|
|
Hello Dave, Thanks for Bjoern's suggestion. By the way, have you tied with XPathNavigator.Select method? This method will return a node set. We could check the count property of this set. If there is no node exists, the number will be Zero. XPathDocument document = new XPathDocument("XMLFile1.xml"); XPathNavigator navigator = document.CreateNavigator(); XPathNodeIterator nodes = navigator.Select("/bookstore/book1"); if (nodes.Count == 0) {// the node doesn't exist. } else {... } Addiotnally, this method would used with compile. XPathDocument document = new XPathDocument("XMLFile1.xml"); XPathNavigator navigator = document.CreateNavigator(); XPathExpression expr = navigator.Compile("true()"); switch (expr.ReturnType) { case XPathResultType.NodeSet: { XPathNodeIterator iterator = navigator.Select(expr); break; } case XPathResultType.Boolean: { bool f = (bool)navigator.Evaluate(expr); break; } case XPathResultType.Number: { .... break; } case XPathResultType.String: { ... break; } default: { ... break; } } Hope this helps. Please feel free to update here if there is anything we can help with. We are glad to assist you. Have a great weekend, Sincerely, Wen Yuan Microsoft Online Community Support ================================================== This posting is provided "AS IS" with no warranties, and confers no rights. |
#13
| |||
| |||
|
|
Hello Dave, Thanks for Bjoern's suggestion. By the way, have you tied with XPathNavigator.Select method? This method will return a node set. We could check the count property of this set. If there is no node exists, the number will be Zero. XPathDocument document = new XPathDocument("XMLFile1.xml"); XPathNavigator navigator = document.CreateNavigator(); XPathNodeIterator nodes = navigator.Select("/bookstore/book1"); if (nodes.Count == 0) {// the node doesn't exist. } else {... } Addiotnally, this method would used with compile. XPathDocument document = new XPathDocument("XMLFile1.xml"); XPathNavigator navigator = document.CreateNavigator(); XPathExpression expr = navigator.Compile("true()"); switch (expr.ReturnType) { case XPathResultType.NodeSet: { XPathNodeIterator iterator = navigator.Select(expr); break; } case XPathResultType.Boolean: { bool f = (bool)navigator.Evaluate(expr); break; } case XPathResultType.Number: { .... break; } case XPathResultType.String: { ... break; } default: { ... break; } } Hope this helps. Please feel free to update here if there is anything we can help with. We are glad to assist you. Have a great weekend, Sincerely, Wen Yuan Microsoft Online Community Support ================================================== This posting is provided "AS IS" with no warranties, and confers no rights. |
#14
| |||
| |||
|
|
Hello Dave, Thanks for Bjoern's suggestion. By the way, have you tied with XPathNavigator.Select method? This method will return a node set. We could check the count property of this set. If there is no node exists, the number will be Zero. XPathDocument document = new XPathDocument("XMLFile1.xml"); XPathNavigator navigator = document.CreateNavigator(); XPathNodeIterator nodes = navigator.Select("/bookstore/book1"); if (nodes.Count == 0) {// the node doesn't exist. } else {... } Addiotnally, this method would used with compile. XPathDocument document = new XPathDocument("XMLFile1.xml"); XPathNavigator navigator = document.CreateNavigator(); XPathExpression expr = navigator.Compile("true()"); switch (expr.ReturnType) { case XPathResultType.NodeSet: { XPathNodeIterator iterator = navigator.Select(expr); break; } case XPathResultType.Boolean: { bool f = (bool)navigator.Evaluate(expr); break; } case XPathResultType.Number: { .... break; } case XPathResultType.String: { ... break; } default: { ... break; } } Hope this helps. Please feel free to update here if there is anything we can help with. We are glad to assist you. Have a great weekend, Sincerely, Wen Yuan Microsoft Online Community Support ================================================== This posting is provided "AS IS" with no warranties, and confers no rights. |
#15
| |||
| |||
|
|
Hello Dave, Thanks for Bjoern's suggestion. By the way, have you tied with XPathNavigator.Select method? This method will return a node set. We could check the count property of this set. If there is no node exists, the number will be Zero. XPathDocument document = new XPathDocument("XMLFile1.xml"); XPathNavigator navigator = document.CreateNavigator(); XPathNodeIterator nodes = navigator.Select("/bookstore/book1"); if (nodes.Count == 0) {// the node doesn't exist. } else {... } Addiotnally, this method would used with compile. XPathDocument document = new XPathDocument("XMLFile1.xml"); XPathNavigator navigator = document.CreateNavigator(); XPathExpression expr = navigator.Compile("true()"); switch (expr.ReturnType) { case XPathResultType.NodeSet: { XPathNodeIterator iterator = navigator.Select(expr); break; } case XPathResultType.Boolean: { bool f = (bool)navigator.Evaluate(expr); break; } case XPathResultType.Number: { .... break; } case XPathResultType.String: { ... break; } default: { ... break; } } Hope this helps. Please feel free to update here if there is anything we can help with. We are glad to assist you. Have a great weekend, Sincerely, Wen Yuan Microsoft Online Community Support ================================================== This posting is provided "AS IS" with no warranties, and confers no rights. |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |