how to iterate Data Table

Asked By AJMAL YAZDANI
02-Sep-10 11:51 AM
Earn up to 0 extra points for answering this tough question.

I have below DataTable with 2 columns (AttrName and DataType) ,

AttrName

DataType

Company_ID

Decimal(15,0)

Company_Name

Varchar(500)

 

I am preparing an XML schema base on above data so I need to iterate the above DataTable and need string like below,

Please help me how can I iterate and make string like below…

string str = string.Empty;

 

        str += "<xsd:element name=\"Company_ID\" type=\"xsd:decimal\" />";

        str += "<xsd:element name=\"Company_Name\" type=\"xsd:string\" />";

 

  re: how to iterate Data Table

Venkat K replied to AJMAL YAZDANI
02-Sep-10 12:09 PM
You can use the following loop to fetch the column names:

// Get the column names and their types.
for (int j=0; j< dvColumns.Count; j++)
{
// Get the name of the column.
drvCols = dvColumns[j];
sColName = drvCols.Row.ItemArray[3].ToString();

later you can concatenate the other xsd elements to the existing column names.

Thanks

  re: how to iterate Data Table

Web Star replied to AJMAL YAZDANI
02-Sep-10 12:54 PM

If you want to loop through a DataTable, here's one way of doing it:


foreach (DataRow dataRow in DataTable1.Rows) {


    foreach (DataColumn dataColumn in DataTable1.Columns) {


      string val = dataRow[dataColumn].ToString();


   }


}


Create New Account