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. how do i get a node position in an xml file

how do i get a node position in an xml file

Scheduled Pinned Locked Moved C#
xmltutorialquestion
7 Posts 4 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    im trying to make an xml search method for example this is my xml file what i wanna do its get movinh trought the xml and when i get the looking for attribute the method will give me back an string with this caracteristics d=0&n=1 where d its equal to the current node and n its equal to the current item node where the looking attribute was found for example d=0&n=1 the atribute nombre = calendario but if i im on a child of a item node for example the result it will be d=1&n=0,0 please if some one can helpme plese email me quickly

    L 1 Reply Last reply
    0
    • L Lost User

      im trying to make an xml search method for example this is my xml file what i wanna do its get movinh trought the xml and when i get the looking for attribute the method will give me back an string with this caracteristics d=0&n=1 where d its equal to the current node and n its equal to the current item node where the looking attribute was found for example d=0&n=1 the atribute nombre = calendario but if i im on a child of a item node for example the result it will be d=1&n=0,0 please if some one can helpme plese email me quickly

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2
      M 1 Reply Last reply
      0
      • L Lost User
        M Offline
        M Offline
        mav northwind
        wrote on last edited by
        #3

        Try checking the "Do not treat <'s as HTML tags" checkbox so we can actually read your xml... mav

        A 1 Reply Last reply
        0
        • M mav northwind

          Try checking the "Do not treat <'s as HTML tags" checkbox so we can actually read your xml... mav

          A Offline
          A Offline
          alexdg04
          wrote on last edited by
          #4
          J 1 Reply Last reply
          0
          • A alexdg04
            J Offline
            J Offline
            je_gonzalez
            wrote on last edited by
            #5

            private string DumpIndex(XmlNode node) { string sAns = ""; if (node.ParentNode != null) { int iPos = -1; foreach (XmlNode c_Node in node.ParentNode.ChildNodes) { iPos++; if (c_Node.Equals(node)) { sAns = this.DumpIndex(node.ParentNode); if (sAns.Length > 0) sAns += "."; sAns += iPos; break; } } } return sAns; }

            A 1 Reply Last reply
            0
            • J je_gonzalez

              private string DumpIndex(XmlNode node) { string sAns = ""; if (node.ParentNode != null) { int iPos = -1; foreach (XmlNode c_Node in node.ParentNode.ChildNodes) { iPos++; if (c_Node.Equals(node)) { sAns = this.DumpIndex(node.ParentNode); if (sAns.Length > 0) sAns += "."; sAns += iPos; break; } } } return sAns; }

              A Offline
              A Offline
              alexdg04
              wrote on last edited by
              #6

              i think that the code its perfect but can u explainme how do i know what node i must send to this funtion first for examle string dumindex(myfirsnode)

              J 1 Reply Last reply
              0
              • A alexdg04

                i think that the code its perfect but can u explainme how do i know what node i must send to this funtion first for examle string dumindex(myfirsnode)

                J Offline
                J Offline
                je_gonzalez
                wrote on last edited by
                #7

                using System.Xml; XmlDocument c_Doc = new XmlDocument(); c_Doc.Load(...) or LoadXml(...); XmlNodeList c_List = c_Doc.DocumentElement.SelectNodes("//*[nombre='???']"); ... or ... c_Doc.DocumentElement.SelectNodes("//item[nombre='???']"); foreach (XmlNode c_Node in c_List) { ... Do the DumpIndex with c_Node and whatever else ... } I am writing this away from my computer, so I cannot check it. Use the first SelectNodes methods to include all nodes, the second if you want to include only the item nodes. Of ccource the ??? needs to be replaced with the name that you are searching for. Anyway, you should be able to get the idea on how to use XPath (If you need to learn more, check the w3schools.com site)

                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