|
-
May 18th, 2011, 06:49 AM
#1
Thread Starter
Fanatic Member
[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
-
May 18th, 2011, 06:57 AM
#2
Thread Starter
Fanatic Member
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?
-
May 18th, 2011, 07:59 AM
#3
Thread Starter
Fanatic Member
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
-
May 18th, 2011, 01:33 PM
#4
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
-
May 19th, 2011, 08:47 AM
#5
Thread Starter
Fanatic Member
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!
-
May 19th, 2011, 09:05 AM
#6
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
-
May 19th, 2011, 11:04 AM
#7
Thread Starter
Fanatic Member
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
-
May 21st, 2011, 09:46 AM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|