C# .NET - how to iterate Data Table

Asked By AJMAL YAZDANI
02-Sep-10 11:51 AM

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\" />";

 

  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
  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
help
www.teresablanco.com / Listing / VirtualTour.aspx?ListingID = 958035< / VirtualTourUrl> <Location> <GeoArea id = "207550" type = "Neighborhood" name = "Midtown" / > <GeoArea id = "44569" type = "City" name = "Atlanta" / > <GeoArea id = "289" type = "State" name = "Georgia" abbreviation = "GA" / > <GeoArea id = "44" type = "Country" name = "United States" abbreviation = "US" / > <Address publiclyVisible = "true" suite = "1505"> 120 Ralph McGill Blvd< / Address> <PostalCode p2a / listing / 8842 / 1168 / 8e2b / e709c93a42ec0f0a5eb5 / w64h48.jpg< / Url> < / Item> < / Media> <ListingAgent> <AgentID> 216636< / AgentID> <Name> Teresa Blanco< / Name> <ContactInfo> <Telephone name = "Work"> 404-664-4927< / Telephone> <WebSite> http: / / www.teresablanco.com< / WebSite> <ProfileUrl> http: / / NLS.point2 point2.com / p2a / agency / bf4c / 9c4b / 9561 / 638f22f013dbc6597213 / w160h120.jpg< / Url> < / Item> < / Media> < / ListingAgent> <Broker> <Name> Blue Sky Real Estate Group< / Name> < / Broker> < / Listing> <Listing> <ListingID> 958041< / ListingID> <RegionalMLSNumber publiclyVisible = "1
Sample XML file <?xml version = "1.0" encoding = "utf-8"?> < params > < param > < value > < struct > < member > < name > result_count< / name > < value > < int > 1< / int > < / value > < / member > < member > < name > next_offset< / name > < value > < int > 1< / int > < / value > < / member > < member > < name > field_list< / name > < value > < array > < data > < value > < struct > < member > < name > name< / name > < value > < string > id< / string > < / value > < / member > < member > < name > type< / name > < value > < string > id< / string
pure output parameter (derived as InputOutput) / / / where the user provided no input value. / / / < / summary> / / / <param name = "command"> The command to which the parameters will be added< / param> / / / <param name = "commandParameters"> An array of SqlParameters to be added to command< / param> private static void AttachParameters p.Value = = null)) { p.Value = DBNull.Value; } command.Parameters.Add(p); } } } } / / / <summary> / / / This method assigns dataRow column values to an array of SqlParameters / / / < / summary> / / / <param name = "commandParameters"> Array of SqlParameters to be assigned values< / param> / / / <param name = "dataRow"> The dataRow used to hold the stored procedure's parameter values< / param> private static void AssignParameterValues(SqlParameter commandParameters, DataRow dataRow) { if ((commandParameters = = null) | | (dataRow = = null)) { / / Do nothing if we get no data return; } int
CREATE TABLE [T_SPACEUSEAGREEMENT] ( [spec_id] [numeric](20, 0) NOT NULL , [Sys_projectid] [numeric](20, 0) NULL , [Sys_objectstate] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CS_AS NULL , [Sys_objectId] [numeric](20, 0) NULL , [Sys_guiid] [numeric](20, 0) NULL , [Sys_ReviewStatus] [varchar] (1000) COLLATE SQL_Latin1_General_CP1_CS_AS NULL , [Sys_PriorState] [varchar] (1000) COLLATE SQL_Latin1_General_CP1_CS_AS NULL , [ce_Security] [numeric](32, 12) NULL , [ce_Security_UOM varchar] (100) COLLATE SQL_Latin1_General_CP1_CS_AS NULL , [ce_PropertyTaxes] [numeric](32, 12) NULL , [ce_PropertyTaxes_UOM] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CS_AS NULL , [ce_Insurance] [numeric](32, 12) NULL , [ce_Insurance_UOM] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CS_AS NULL , [ce_Electricity] [numeric](32, 12) NULL , [ce_Electricity_UOM] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CS_AS NULL , [ce_UtilitiesBase] [numeric](32, 12) NULL , [ce_UtilitiesBase_UOM] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CS_AS NULL , [ce_SecurityBase] [numeric](32, 12) NULL , [ce_SecurityBase_UOM] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CS_AS NULL
UN) - -> <xs:schema xmlns:xs = " http: / / www.w3.org / 2001 / XMLSchema " id = "STR"> <xs:element name = "STR"> <xs:complexType> <xs:sequence> <xs:element name = "t_report"> <xs:complexType> <xs:sequence> <xs:element name = "rentity_id"> <xs:simpleType> <xs:restriction base = "xs:int"> <xs:minInclusive value = "1" / > < / xs:restriction> < / xs simpleType> < / xs:element> <xs:element name = "submission_code" type = "submission_type" / > <xs:element name = "report_code" type = "xs:string" fixed = "STR" / > <xs:element name = "entity_reference" minOccurs = "0"> <xs:simpleType> <xs:restriction base = "xs:string"> <xs:maxLength value = "20" / > < / xs:restriction> < / xs:simpleType> < / xs:element> <xs:element name = "submission_date" type = "sql_date" / > <xs:sequence maxOccurs = "unbounded"> <xs:element name = "transaction"> <xs:complexType> <xs:sequence> <xs:element name = "transactionnumber"> <xs:simpleType> <xs:restriction base = "xs