Results 1 to 8 of 8

Thread: [RESOLVED] xpath iteration

  1. #1

    Thread Starter
    Fanatic Member davebat's Avatar
    Join Date
    Dec 2002
    Posts
    727

    Resolved [RESOLVED] xpath iteration

    Hi I am using xpath to find nodes in xml and bind to a radiobuttonlist, so far I have

    Code:
     Dim Doc As XPathDocument = New XPathDocument(localDrive & "\xml\jobs.xml")
                    Dim Navigator As XPathNavigator
                    Navigator = Doc.CreateNavigator()
                    'Dim Iterator As XPathNodeIterator = Navigator.Select("/jobCodes/job[SoRCode[text()='" & Master.SORcode & "']]/DUIJobDescription")
                    Dim Iterator As XPathNodeIterator = Navigator.Select("/jobCodes/job[SoRCode[text()='" & Master.SORcode & "']][RoomID[text()='" & Master.roomID & "']]/DUIJobDescription")
                    While Iterator.MoveNext()
                        rblJobs.Items.Add(New ListItem(Iterator.Current.Value, Iterator.Current.Value))
                    End While
    The problem is this is binding the same value to both the text and value of the radiobutton list. How would I pull another node from the xml and display this as the value?

    Thanks in advance

  2. #2

    Thread Starter
    Fanatic Member davebat's Avatar
    Join Date
    Dec 2002
    Posts
    727

    Re: xpath iteration

    Figured out how top get other values;

    /jobCodes/job[RoomID[text()='" & Master.roomID & "']][ComponentID[text()='" & Master.CompID & "']]/DUIJobDescription | /jobCodes/job[RoomID[text()='" & Master.roomID & "']][ComponentID[text()='" & Master.CompID & "']]/jobCode

    But this doesnt do it in pairs so i end up with two radiobuttons, one with DUIJobDescription and one with jobCode.

    Any ideas?

  3. #3

    Thread Starter
    Fanatic Member davebat's Avatar
    Join Date
    Dec 2002
    Posts
    727

    Re: xpath iteration

    Well im stumped, so i cheated a bit

    Code:
    While Iterator.MoveNext()
                        If i Mod 2 = 0 Then
                            rblJobs.Items.Add(New ListItem(Iterator.Current.Value, Iterator.Current.Value))
                        Else
                            rblJobs.Items(i - 1).Value = Iterator.Current.Value
                        End If
                        i = i + 1
                    End While
    If anyone has a more elegant solution I'd love to hear it

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] xpath iteration

    Personally, when dealing with XPath, I always recommend this tool:

    http://www.bubasoft.net/xpathbuilder/Xpathbuilder2.aspx

    Can you upload a sample of the XML file you are using?

    Gary

  5. #5

    Thread Starter
    Fanatic Member davebat's Avatar
    Join Date
    Dec 2002
    Posts
    727

    Re: [RESOLVED] xpath iteration

    Here you are Gep, thanks. (I know the end nodes missing)

    Code:
    <jobCodes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    	<job>
    		<RoomID>1</RoomID>
    		<RoomName>Bathroom / Toilet</RoomName>
    		<ComponentID>2</ComponentID>
    		<Componentname>Bath</Componentname>
    		<DUIJobDescription>Renew white mastic seal around bath</DUIJobDescription>
    		<SoRCode>whi_mas_bat_mt1</SoRCode>
    		<TradeType>Multi In</TradeType>
    		<SkillLevelRequired>2</SkillLevelRequired>
    		<SuitableTrades>MT int, MT ext</SuitableTrades>
    		<notes>Jobs which a specialist or MT can do</notes>
    	</job>
    	<job>
    		<RoomID>1</RoomID>
    		<RoomName>Bathroom / Toilet</RoomName>
    		<ComponentID>2</ComponentID>
    		<Componentname>Bath</Componentname>
    		<DUIJobDescription>Repair damaged / chipped bath</DUIJobDescription>
    		<SoRCode>dam_chi_bat_pl3</SoRCode>
    		<TradeType>Plumbing</TradeType>
    		<SkillLevelRequired>3</SkillLevelRequired>
    		<SuitableTrades>Plumber</SuitableTrades>
    		<notes>Jobs which only a specialist can do</notes>
    	</job>
    	<job>
    		<RoomID>1</RoomID>
    		<RoomName>Bathroom / Toilet</RoomName>
    		<ComponentID>2</ComponentID>
    		<Componentname>Bath</Componentname>
    		<DUIJobDescription>Repair damaged bath panel</DUIJobDescription>
    		<SoRCode>dam_bat_pan_mt3</SoRCode>
    		<TradeType>Multi In</TradeType>
    		<SkillLevelRequired>3</SkillLevelRequired>
    		<SuitableTrades>MT int</SuitableTrades>
    		<notes>Jobs which only a specialist can do</notes>
    	</job>
    Its the most basic xml i could make, but please don't spend too much time on this on my behalf, I've got loads more weird and wonderful wsHttpBinding problems to keep me occupied today!

  6. #6
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] xpath iteration

    So, just to confirm, can you give a quick few words about what you are trying to get out of the XML, and what you want to do with it?

    Gary

  7. #7

    Thread Starter
    Fanatic Member davebat's Avatar
    Join Date
    Dec 2002
    Posts
    727

    Re: [RESOLVED] xpath iteration

    I want to find a particular node for job based on roomId and componentID and then pull out SorCOde and description to bind to a radiobuttonlist as text and value respectively

  8. #8
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] xpath iteration

    Hey,

    As with most things XML related, there are multiple ways to skin a cat.

    Here is another way of getting the information that you need:

    Code:
            Dim xpathDoc As XPathDocument = New XPathDocument(Server.MapPath("XmlFile1.xml"))
            Dim navigator As XPathNavigator = xpathDoc.CreateNavigator()
            Dim expression As XPathExpression = navigator.Compile("/jobCodes/job[RoomID['1']][ComponentID['2']]")
            Dim nodeIterator As XPathNodeIterator = navigator.Select(expression)
    
            While nodeIterator.MoveNext()
                Dim clone As XPathNavigator = nodeIterator.Current.Clone()
                clone.MoveToChild("DUIJobDescription", String.Empty)
                Dim duiDescription As String = clone.Value
                clone.MoveToParent()
    
                clone.MoveToChild("SoRCode", String.Empty)
    
                Dim sorCode As String = clone.Value
    
                Response.Write(String.Format("DUIDescription: {0}, SorCode: {1}", duiDescription, sorCode))
            End While
    Here, I am only get a reference to the parent node, in this case "job" then navigating the navigator to get the information that is one level down from it.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width