xml parsing - Read XML Webfile in C# Windows Form and place data in labels -
for information program need read web xml file , place in label every value.
an example of xml file:
<actuelevertrektijden> <vertrekkendetrein> <ritnummer>5070</ritnummer> <vertrektijd>2015-03-20t19:42:00+0100</vertrektijd> <eindbestemming>den haag centraal</eindbestemming> <treinsoort>sprinter</treinsoort> <routetekst>lage zwaluwe, dordrecht, rotterdam c.</routetekst> <vervoerder>ns</vervoerder> <vertrekspoor wijziging="false">6</vertrekspoor> </vertrekkendetrein> <vertrekkendetrein> <ritnummer>1971</ritnummer> <vertrektijd>2015-03-20t19:50:00+0100</vertrektijd> <eindbestemming>venlo</eindbestemming> <treinsoort>intercity</treinsoort> <routetekst>tilburg, eindhoven, helmond</routetekst> <vervoerder>ns</vervoerder> <vertrekspoor wijziging="false">4</vertrekspoor> <opmerkingen> // not available, important , specific 'vertrekkendetrein' <opmerking>rijdt vandaag niet</opmerking> </opmerkingen> </vertrekkendetrein> </actuelevertrektijden>
i tried xmldocument (using foreach) this:
foreach (xmlnode nodelist2 in nodelist) { if (i < 1) //1 { switch (nodelist.name) { case "vertrektijd": string vertrek1 = (nodelist2.innertext); break; case "vertrekvertragingstekst": string vertraging1 = (nodelist2.innertext); break; case "eindbestemming": string eindbest1 = (nodelist2.innertext); break; case "treinsoort": string treinsoort1 = (nodelist2.innertext); break; case "routetekst": string route1 = (nodelist2.innertext); break; case "vertrekspoor": string spoor1 = (nodelist2.innertext); i++ break; case "opmerkingen": case "opmerking": string opmerking1 = (nodelist2.innertext); break; } } }
but wasn't success. there smarter way read it, , place in lot of labels?
edit: answer got, tried following code:
try { string urlo = "**secured webaddress not end .xml**"; string resultje = httpgeto(urlo); xmldocument doc = new xmldocument(); xmlnode root = doc.firstchild; //* document has 1 root element "actuelevertrektijden" //* root element has multiple child nodes "vertrekkendetrein" xmlnodelist nodelist1 = root.childnodes; (int = 0; < nodelist1.count; i++) { xmlnodelist nodelist2 = nodelist1[i].childnodes; //* each child node of of child nodes, //* need text within each 1 of them foreach (xmlnode node in nodelist2) { switch (node.name) { case "vertrektijd": string vertrek1 = node.innertext; messagebox.show(vertrek1); lblts1.text = vertrek1; break; case "vertrekvertragingstekst": string vertraging1 = node.innertext; break; case "eindbestemming": string eindbest1 = node.innertext; break; case "treinsoort": string treinsoort1 = node.innertext; break; case "routetekst": string route1 = node.innertext; break; case "vertrekspoor": string spoor1 = node.innertext; break; case "opmerkingen": xmlnode opnode = node.firstchild; if (opnode != null) { string opmerking1 = opnode.innertext; } break; } } } } catch { lblcatch.text = "werktniet"; }
but doesn't work. , also, how can read next "vertrekkendetrein"?, copying code , use i++;? need read first 6 "vertrekkendetrein".
xmlnode root = doc.firstchild; //* document has 1 root element "actuelevertrektijden" //* root element has multiple child nodes "vertrekkendetrein" xmlnodelist nodelist1 = root.childnodes; (int i=0; i<nodelist1.count; i++) { xmlnodelist nodelist2 = nodelist1[i].childnodes; //* each child node of of child nodes, //* need text within each 1 of them foreach (xmlnode node in nodelist2) { switch (node.name) { case "vertrektijd": string vertrek1 = node.innertext; break; case "vertrekvertragingstekst": string vertraging1 = node.innertext; break; case "eindbestemming": string eindbest1 = node.innertext; break; case "treinsoort": string treinsoort1 = node.innertext; break; case "routetekst": string route1 = node.innertext; break; case "vertrekspoor": string spoor1 = node.innertext; break; case "opmerkingen": xmlnode opnode = node.firstchild; if(opnode!=null) { string opmerking1 = opnode.innertext; } break; } } }
Comments
Post a Comment