Reading XML Sub Items in VB.NET -
please have in xml file:
<?xml version="1.0" encoding="utf-8"?> <!--xml database.--> <data> <student> <name>john e.</name> <tests> <test> <lesson>mathematics</lesson> <grade>a</grade> </test> <test> <lesson>physics</lesson> <grade>a+</grade> </test> </tests> </student> <!-- sidenote: jessica didn't attend physics exam--> <student> <name>jessica b</name> <tests> <test> <lesson>mathematics</lesson> <grade>b</grade> </test> </tests> </student> </data>
i'm trying display tests each student had. have 1 textbox in application(textbox1) , want type "john e." or "jessica b." , give me tests have made.
already added listview 2 colums: lesson, grade.
i tried many ways yet find 1 that's working. writing such xml structure easy can't reading work. appreciate help.
please note im using code: http://forum.codecall.net/topic/69450-writing-and-reading-xml-files-vbnet-part-ii can base answer on , can save time.
this need code: 2 buttons (one "search" , 1 clear textbox , listview fields) , textbox , listview.
the form load
sets listview columns:
private sub form1_load(sender object, e eventargs) handles mybase.load listview1.columns.add("lesson", 100, horizontalalignment.left) listview1.columns.add("grade", 150, horizontalalignment.left) end sub
and search button click event:
private sub button1_click(sender object, e eventargs) handles button1.click dim isstudent boolean = false dim xmldoc xmldocument = new xmldocument() dim str(2) string try xmldoc.load("put xml file path here!") catch ex exception msgbox("xml loading failled : " & vbcrlf & ex.message & vbcrlf) end try dim element xmlnodelist try element = xmldoc.documentelement.getelementsbytagname("student") each datanode in element //go data node if datanode.localname = "student" each studentnode in datanode.childnodes //loops student childnodes if studentnode.localname = "name" if studentnode.innertext.tostring.trim.equals(textbox1.text) isstudent = true else isstudent = false end if elseif studentnode.localname = "tests" , isstudent each tests in studentnode.childnodes //loops tests childnodes... if tests.localname = "test" each test in tests.childnodes //loops test childnodes if test.localname = "lesson" str(0) = test.innertext.tostring.trim elseif test.localname = "grade" str(1) = test.innertext.tostring.trim end if next listview1.items.add(new listviewitem({str(0), str(1)})) end if next end if next end if next catch ex exception msgbox("xml reading failed @ element: " & vbcrlf & ex.message & vbcrlf) end try end sub
the clear button let continuously looking new student:
private sub button2_click(sender object, e eventargs) handles button2.click textbox1.text = "" listview1.clear() listview1.columns.add("lesson", 100, horizontalalignment.left) listview1.columns.add("grade", 150, horizontalalignment.left) end sub
try study code , understand how xml reading works. seems difficult way same, depends on xml structure: nodes , childnodes. same! there must simplest way that, practice programming.
Comments
Post a Comment