C# Error Displaying The Xml Element in DataGridView -
the purpose of code reading xml elements , display every element under related column name in datagridview.
so, here have code:
ienumerable<xelement> tables = xelement.elements(df + "table"); foreach (xelement table in tables) { //get name tablename node xelement tablenamenode = table.element(df + "tablename"); tbtablename.text = tablenamenode.value.tostring(); xelement numberrows = table.element(df + "numberofrows"); tbnumrows.text = numberrows.value.tostring(); string tablename = tablenamenode.value; tabledata td = new tabledata(currentproject.currentschema.findtable(tablename)); td.populatefielddata(); // tabledatalist.trygetvalue(tablename, out td); tabledatalist.add(tablename, td); //if open project after save or open it, exception throws: added key! ienumerable<xelement> fields = table.elements(df + "field"); foreach (xelement field in fields) { xelement fieldnamenode = field.element(df + "name"); string fieldname = fieldnamenode.value; fielddata fd = td.fieldlist[fieldname]; ienumerable<xelement> fieldprops = field.descendants(); foreach (xelement fieldprop in fieldprops) { string fieldpropertyname = fieldprop.name.tostring(); if (fieldpropertyname == "name") { fd.name = fieldprop.value; } if (fieldpropertyname == "type") { fd.datatype = fieldprop.value; } if (fieldpropertyname == "size") { int = 0; int.tryparse(fieldprop.value.tostring(), out i); fd.size = i; } if (fieldpropertyname == "nullable") { if (fieldprop.value.tostring() == "true") fd.nullable = true; else fd.nullable = false; } if (fieldpropertyname == "contentsource") { fd.contentsource = fieldprop.value; } if (fieldpropertyname == "constantvalue") { fd.constantvalue = fieldprop.value; } if (fieldpropertyname == "randomvalue") { fd.averagesize = fieldprop.value; } if (fieldpropertyname == "list") { fd.picklist = fieldprop.value; } } datagridview1.rows.add(fd.name, fd.datatype, fd.size, fd.nullable); string columnname = datagridview1.currentrow.cells[0].value.tostring(); this.tabledatalist.trygetvalue(tbtablename.text, out td); if (td != null) { td.fieldlist.trygetvalue(columnname, out fd); } foreach (var fdl in td.fieldlist) { if (td != null && fd != null) { if (fd.contentsource == "constant") { datagridview1.currentrow.cells[4].value = fd.contentsource + "(" + "value= " + fd.constantvalue + ")"; } if (fd.contentsource == "list") { datagridview1.currentrow.cells[4].value = fd.contentsource + "(" + fd.picklist + ")"; } if (fd.contentsource == "random") { datagridview1.currentrow.cells[4].value = fd.contentsource + "(" + fd.averagesize + ")"; } } } } }
here xml file reading:
<?xml version="1.0"?> <generatorxml> <schemapath>c:\projects\coreschema.xml</schemapath> <groupname>documents</groupname> -<table> <tablename>directory</tablename> <numberofrows>33</numberofrows> -<field> <name>dir_id</name> <type>number</type> <size>10</size> <nullable>false</nullable> <contentsource>random</contentsource> <constantvalue> </constantvalue> <randomvalue>4</randomvalue> <list> </list> </field> -<field> <name>directory_number</name> <type>number</type> <size>5</size> <nullable>false</nullable> <contentsource>list</contentsource> <constantvalue> </constantvalue> <randomvalue> </randomvalue> <list>coffee</list> </field> -<field> <name>file_count</name> <type>number</type> <size>5</size> <nullable>false</nullable> <contentsource>constant</contentsource> <constantvalue>2</constantvalue> <randomvalue> </randomvalue> <list> </list> </field> +<field> </table> </generatorxml>
i think loops got problem, can't find it. in advance every thoughts.
Comments
Post a Comment