public DataTable ConvertStringToDataTable(string xmlString, int i = 0)
{
string[] strXmlString = xmlString.Split('*');
string xmlDataString = strXmlString[0];
string xmlCaptionString = strXmlString[1];
string[] arrCaption = xmlCaptionString.Split('#');
string xmlDataTypeString = strXmlString[2];
string[] arrDataType = xmlDataTypeString.Split('#');
DataSet dataSet = new DataSet("HDA" + i);
StringReader stringReader = new StringReader(xmlDataString);
dataSet.ReadXml(stringReader);
dataSet.Tables[0].TableName = "product" + i;
for (int j = 0; j < dataSet.Tables[0].Columns.Count; j++)
{
dataSet.Tables[0].Columns[j].Caption = arrCaption[j];
}
DataTable dtCloned = dataSet.Tables[0].Clone();
for (int j = 0; j < dataSet.Tables[0].Columns.Count; j++)
{
switch (arrDataType[j])
{
case "Double":
dtCloned.Columns[j].DataType = typeof(double);
break;
case "String":
dtCloned.Columns[j].DataType = typeof(string);
break;
case "DateTime":
dtCloned.Columns[j].DataType = typeof(DateTime);
break;
case "Int32":
dtCloned.Columns[j].DataType = typeof(Int32);
break;
default:
dtCloned.Columns[j].DataType = typeof(double);
break;
}
}
DataTable dtTemp = dataSet.Tables[0];
DataRow drCloned;
foreach (DataRow row in dtTemp.Rows)
{
drCloned = dtCloned.NewRow();
for (int colIndex = 0; colIndex < dtTemp.Columns.Count; colIndex++)
{
if (row[colIndex].Equals(string.Empty))
{
drCloned[colIndex] = DBNull.Value;
}
else
{
drCloned[colIndex] = row[colIndex];
}
}
dtCloned.Rows.Add(drCloned);
}
return dtCloned;
}
{
string[] strXmlString = xmlString.Split('*');
string xmlDataString = strXmlString[0];
string xmlCaptionString = strXmlString[1];
string[] arrCaption = xmlCaptionString.Split('#');
string xmlDataTypeString = strXmlString[2];
string[] arrDataType = xmlDataTypeString.Split('#');
DataSet dataSet = new DataSet("HDA" + i);
StringReader stringReader = new StringReader(xmlDataString);
dataSet.ReadXml(stringReader);
dataSet.Tables[0].TableName = "product" + i;
for (int j = 0; j < dataSet.Tables[0].Columns.Count; j++)
{
dataSet.Tables[0].Columns[j].Caption = arrCaption[j];
}
DataTable dtCloned = dataSet.Tables[0].Clone();
for (int j = 0; j < dataSet.Tables[0].Columns.Count; j++)
{
switch (arrDataType[j])
{
case "Double":
dtCloned.Columns[j].DataType = typeof(double);
break;
case "String":
dtCloned.Columns[j].DataType = typeof(string);
break;
case "DateTime":
dtCloned.Columns[j].DataType = typeof(DateTime);
break;
case "Int32":
dtCloned.Columns[j].DataType = typeof(Int32);
break;
default:
dtCloned.Columns[j].DataType = typeof(double);
break;
}
}
DataTable dtTemp = dataSet.Tables[0];
DataRow drCloned;
foreach (DataRow row in dtTemp.Rows)
{
drCloned = dtCloned.NewRow();
for (int colIndex = 0; colIndex < dtTemp.Columns.Count; colIndex++)
{
if (row[colIndex].Equals(string.Empty))
{
drCloned[colIndex] = DBNull.Value;
}
else
{
drCloned[colIndex] = row[colIndex];
}
}
dtCloned.Rows.Add(drCloned);
}
return dtCloned;
}
No comments:
Post a Comment