Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Simple XPath Question

Simple XPath Question

Scheduled Pinned Locked Moved C#
xmlquestionhelptutorial
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    antoine orchus tech
    wrote on last edited by
    #1

    Hi, Having not found a good XPath tutorial out there yet, here is my question.. This is how I try to display the fisrtname of the first contact in the Contact.xml file, Consider this code: XmlDocument myDoc = new XmlDocument(); myDoc.Load(Application.StartupPath + "\\Contact.xml"); txtPrénom.Text = myDoc.SelectSingleNode("//Prenom").Value; What am I missing here? Thank you for your help! Antoine This by our hands that dream, "I shall find a way or make one!"

    C T J 3 Replies Last reply
    0
    • A antoine orchus tech

      Hi, Having not found a good XPath tutorial out there yet, here is my question.. This is how I try to display the fisrtname of the first contact in the Contact.xml file, Consider this code: XmlDocument myDoc = new XmlDocument(); myDoc.Load(Application.StartupPath + "\\Contact.xml"); txtPrénom.Text = myDoc.SelectSingleNode("//Prenom").Value; What am I missing here? Thank you for your help! Antoine This by our hands that dream, "I shall find a way or make one!"

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      try txtPrénom.Text = myDoc.SelectSingleNode("//Prenom[first()]").Value; Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder

      1 Reply Last reply
      0
      • A antoine orchus tech

        Hi, Having not found a good XPath tutorial out there yet, here is my question.. This is how I try to display the fisrtname of the first contact in the Contact.xml file, Consider this code: XmlDocument myDoc = new XmlDocument(); myDoc.Load(Application.StartupPath + "\\Contact.xml"); txtPrénom.Text = myDoc.SelectSingleNode("//Prenom").Value; What am I missing here? Thank you for your help! Antoine This by our hands that dream, "I shall find a way or make one!"

        T Offline
        T Offline
        Tomas Petricek
        wrote on last edited by
        #3

        SelectSingleNode returns first node matching XPath so your code should IMHO work, but only if "Prenom" is element (not attribute). To select attrbute you need to use this XPath //@Prenom. You can also use xpath like following:

        Xml:

        <contacts>
        <contact firstname="Bill">
        ...
        <contact>
        ...
        </contacts>

        XPath: contacts/contact/@firstname returns 'Bill'

        Great XPath tutorial is at www.w3schools.com[^].

        Tomáš Petříček  :baaaa!: EeekSoft Tools www.eeeksoft.net

        H 1 Reply Last reply
        0
        • T Tomas Petricek

          SelectSingleNode returns first node matching XPath so your code should IMHO work, but only if "Prenom" is element (not attribute). To select attrbute you need to use this XPath //@Prenom. You can also use xpath like following:

          Xml:

          <contacts>
          <contact firstname="Bill">
          ...
          <contact>
          ...
          </contacts>

          XPath: contacts/contact/@firstname returns 'Bill'

          Great XPath tutorial is at www.w3schools.com[^].

          Tomáš Petříček  :baaaa!: EeekSoft Tools www.eeeksoft.net

          H Offline
          H Offline
          hravn
          wrote on last edited by
          #4

          Better tutorial at http://www.zvon.org/xxl/XSLTreference/Output/index.html[^] ... ;)

          1 Reply Last reply
          0
          • A antoine orchus tech

            Hi, Having not found a good XPath tutorial out there yet, here is my question.. This is how I try to display the fisrtname of the first contact in the Contact.xml file, Consider this code: XmlDocument myDoc = new XmlDocument(); myDoc.Load(Application.StartupPath + "\\Contact.xml"); txtPrénom.Text = myDoc.SelectSingleNode("//Prenom").Value; What am I missing here? Thank you for your help! Antoine This by our hands that dream, "I shall find a way or make one!"

            J Offline
            J Offline
            John Arlen
            wrote on last edited by
            #5

            You have the right idea, but it depends on your actual XML. I suggest getting the node directly and looking at it. This will help answer your question. DogCat XmlNode node = doc.SelectSingleNode("//Prenom"); node.Value == null node.InnerText == "Dog"

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups