xml - VB.Net, XPath and XMLReader - Next and Previous records -


the below vb.net code works fine regards moving next record within xml file (sampler_01.xml). can see, vb.net code uses xpath. problem having getting logic work "previous" button (btnprevious) , getting previous record.

what i'm looking have ability move next 1 record or move previous record. @ point, i'm not sure how logic needs applied. suggestions or appreciated.

thank you!

edit: please see updated answer below "form1.vb 2.0"

"sampler_01.xml"

<?xml version="1.0" encoding="utf-8"?> <companies>     <company id="000001">         <compid>000001</compid>         <contfn>jerry</contfn>         <contln>mcguire</contln>         <contemail>jerrym@email.com</contemail>         <contphone>(603) 555-1212</contphone>     </company>     <company id="000002">         <compid>000002</compid>         <contfn>ben</contfn>         <contln>henderson</contln>         <contemail>benh@email.com</contemail>         <contphone>(603) 882-4040</contphone>     </company>     <company id="000003">         <compid>000003</compid>         <contfn>bob</contfn>         <contln>james</contln>         <contemail>bobj@email.com</contemail>         <contphone>(603) 641-2120</contphone>     </company>     <company id="000004">         <compid>000004</compid>         <contfn>aaron</contfn>         <contln>smith</contln>         <contemail>arrons@email.com</contemail>         <contphone>(603) 968-1212</contphone>     </company> </companies> 

"form1.vb 1.0"

imports system.net imports system.io imports system.xml imports system.xml.xpath    public class form1    dim xpd xpathdocument   dim xpn xpathnavigator   dim xpn1, xpn2, xpn3, xpn4, xpn5 xpathnodeiterator    private sub form1_load(sender system.object, e system.eventargs)   handles mybase.load      loadxmltoread()    end sub    private sub listtotextboxes()      txtcompid.text = xpn1.current.value     txtcontfn.text = xpn2.current.value     txtcontln.text = xpn3.current.value     txtcontemail.text = xpn4.current.value     txtcontphone.text = xpn5.current.value    end sub    private sub loadxmltoread()      xpd = new xpathdocument("c:\xml_files\sampler_01.xml")     xpn = xpd.createnavigator()      xpn1 = xpn.select("/companies/company/compid")     xpn2 = xpn.select("/companies/company/contfn")     xpn3 = xpn.select("/companies/company/contln")     xpn4 = xpn.select("/companies/company/contemail")     xpn5 = xpn.select("/companies/company/contphone")    end sub    private sub btnnext_click(sender system.object, e system.eventargs) handles btnnext.click      if xpn1.movenext() or _        xpn2.movenext() or _        xpn3.movenext() or _        xpn4.movenext() or _        xpn5.movenext()        listtotextboxes()     else      msgbox("end of contacts has been reached...")       loadxmltoread()     end if    end sub    private sub btnload_click(sender system.object, e  system.eventargs)handles btnload.click      loadxmltoread()      if txtcompid.text = "" , _        txtcontfn.text = "" , _        txtcontln.text = "" , _        txtcontln.text = "" , _        txtcontphone.text = ""         xpn1.movenext()        xpn2.movenext()        xpn3.movenext()        xpn4.movenext()        xpn5.movenext()         listtotextboxes()         msgbox("the contact database has been loaded")     end if    end sub    private sub btnprevious_click(sender system.object, e system.eventargs) handles btnprevious.click      if xpn1.current.movetoprevious() or _        xpn2.current.movetoprevious() or _        xpn3.current.movetoprevious() or _        xpn4.current.movetoprevious() or _        xpn5.current.movetoprevious()         listtotextboxes()      end if    end sub end class 

use xml linq

imports system.xml imports system.xml.linq public class form1      const filename string = "c:\temp\test.xml"      sub new()          ' call required designer.         initializecomponent()          ' add initialization after initializecomponent() call.         dim doc xdocument = xdocument.load(filename)          dim companies = doc.descendants("company").tolist()          dim companypointer = companies.first()          dim index integer = 0         companypointer = companies(index + 1)         index = index + 1       end sub  end class ​ 

Comments