none
XML searching the contents based on the key (XPATH) RRS feed

  • Question

  • Hi all of you,

    This is my XML:

    <countries>
      <country code="AF" iso="4">Afghanistan</country>
      <country code="AL" iso="8">Albania</country>
      <country code="DZ" iso="12">Algeria</country>
      <country code="AS" iso="16">American Samoa</country>

    ..

    ..

    </countries>

    Using XPath, how can I retrieve -for example- Albania using prefix "AL"??

    I use these statements when I want to retrieve the value of the key directly (it is other XML):

    <?xml version="1.0" encoding="utf-8"?>
    <PanelesXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Section0>OR</Section0>
    <Section1>Stock de articulos</Section1>
    <Section2>Chapa y pintura</Section2>
    <Section3>Top operarios</Section3>

    ..

    </PanelesXML>

        Dim MyDoc As XPathDocument = New XPathDocument(My.Application.Info.DirectoryPath & "XML\Paneles.xml")
        Dim MyNav As XPathNavigator = MyDoc.CreateNavigator()
        Dim sValue as String = MyNav.SelectSingleNode("//Section0").ToString

    Thank you so much for sharing the knowledge,


    Primary platform is Windows 7 Ultimate 64 bit along with VS 2012/Sql2k8 for WPF/SilverLight projects.

    Wednesday, February 5, 2014 9:52 PM

Answers

  • This XPath would give you the country with the code AL

    /countries/country[@code="AL"]

    I recommend you use a tool to help test your XPath.  I just used this one to test your XML.

    Michael Taylor
    http://msmvps.com/blogs/p3net

    • Marked as answer by Enric Vives Thursday, February 6, 2014 6:55 AM
    Thursday, February 6, 2014 12:56 AM
    Moderator

All replies

  • This does not work:

    proc("AL")

    Sub proc(pais as String)

    Dim MyDoc As XPathDocument = New XPathDocument(My.Application.Info.DirectoryPath & "XML\Paneles.xml")
    Dim MyNav As XPathNavigator = MyDoc.CreateNavigator()

    Dim sValue as String =  MyNav.SelectSingleNode("//countries/country[@code='" & pais & "']")


    Primary platform is Windows 7 Ultimate 64 bit along with VS 2012/Sql2k8 for WPF/SilverLight projects.

    Wednesday, February 5, 2014 10:02 PM
  • This XPath would give you the country with the code AL

    /countries/country[@code="AL"]

    I recommend you use a tool to help test your XPath.  I just used this one to test your XML.

    Michael Taylor
    http://msmvps.com/blogs/p3net

    • Marked as answer by Enric Vives Thursday, February 6, 2014 6:55 AM
    Thursday, February 6, 2014 12:56 AM
    Moderator
  • thanks a lot, it worked perfect

    Primary platform is Windows 7 Ultimate 64 bit along with VS 2012/Sql2k8 for WPF/SilverLight projects.

    Thursday, February 6, 2014 6:55 AM