vb.net - Build XML table using VB Datagridview -
i have web service returns xml.
here example of code returned.
<?xml version="1.0" encoding="utf-8"?> -<response uri="/crm/private/xml/accounts/getrecords"> -<result> -<accounts> -<row no="1"> <fl val="accountid">1202xxx000000121001</fl> <fl val="smownerid">1202xxx000000071001</fl> -<fl val="account owner"> <![cdata[fred smith]]> </fl> -<fl val="account name"> <![cdata[the oaks dental practice]]> </fl> -<fl val="phone"> <![cdata[01202 123123]]> </fl> -<fl val="account number"> <![cdata[0]]> </fl> -<fl val="account type"> <![cdata[prospect]]> </fl> -<fl val="employees"> <![cdata[0]]>
i trying pull data datagridview using vb.net
the trouble returns values based on top level hierarchy, table column heading of "uri" , value of "/crm/private/xml/accounts/getrecords"
if use more simple xml structure, example http://www.w3schools.com/xml/simple.xml works fine.
here vb.net code:
imports system.data imports system.net public class form1 public shared sub datasetsample1() dim request httpwebrequest dim response httpwebresponse = nothing dim dsweather dataset try ' create web request request = directcast(webrequest.create( _ "https://crm.zoho.com/crm/private/xml/accounts/getrecords?authtoken=????????3bb1c27c3116dc6b0275ea91&scope=crmapi"), httpwebrequest) ' response response = directcast(request.getresponse(), httpwebresponse) ' load data dataset dsweather = new dataset() dsweather.readxml(response.getresponsestream()) ' print dataset information printdataset(dsweather) if not response nothing response.close() end try form1.datagridview2.datasource = dsweather.tables(0) end sub public shared sub printdataset(byval ds dataset) ' print out tables , columns each table datatable in ds.tables console.writeline("table '{0}'", table.tablename) console.writeline("total # of rows: {0}", table.rows.count) console.writeline("---------------------------------------------------------------") each column datacolumn in table.columns console.writeline("- {0} ({1})", column.columnname, column.datatype.tostring()) next ' each column console.writeline(system.environment.newline) next ' each table ' print out table relations each relation datarelation in ds.relations console.writeline("relation: {0}", relation.relationname) console.writeline("---------------------------------------------------------------") console.writeline("parent: {0}", relation.parenttable.tablename) console.writeline("child: {0}", relation.childtable.tablename) console.writeline(system.environment.newline) next ' each relation end sub private sub button2_click(sender object, e eventargs) handles button2.click datasetsample1() end sub end class
i looking on how pull correct values build table.
thanks,
try this
imports system.io imports system.xml imports system.xml.linq public class form1 private sub button1_click(sender system.object, e system.eventargs) handles button1.click dim input string = _ "<?xml version=""1.0"" encoding=""utf-8""?>" + _ "<response uri=""/crm/private/xml/accounts/getrecords"">" + _ "<result>" + _ "<accounts>" + _ "<row no=""1"">" + _ "<fl val=""accountid"">1202xxx000000121001</fl>" + _ "<fl val=""smownerid"">1202xxx000000071001</fl>" + _ "<fl val=""account owner"">" + _ "<![cdata[fred smith]]>" + _ "</fl>" + _ "<fl val=""account name"">" + _ "<![cdata[the oaks dental practice]]>" + _ "</fl>" + _ "<fl val=""phone"">" + _ "<![cdata[01202 123123]]>" + _ "</fl>" + _ "<fl val=""account number"">" + _ "<![cdata[0]]>" + _ "</fl>" + _ "<fl val=""account type"">" + _ "<![cdata[prospect]]>" + _ "</fl>" + _ "<fl val=""employees"">" + _ "<![cdata[0]]>" + _ "</fl>" + _ "</row>" + _ "</accounts>" + _ "</result>" + _ "</response>" dim doc xdocument = xdocument.parse(input) dim rows list(of xelement) = doc.descendants("row").tolist() dim rowcount = 1 dim dt new datatable each row in rows if rowcount = 1 each col xelement in row.elements("fl") dim colname string = col.attribute("val").value dt.columns.add(colname, gettype(string)) next col end if rowcount += 1 dim rowdata new list(of string) each col xelement in row.elements("fl") rowdata.add(col.value) next col dt.rows.add(rowdata.toarray()) next row datagridview1.datasource = dt end sub end class
Comments
Post a Comment