Hi Henin,
Thanks for your feedback.
Oh, this lies with how you save the memory xml data into the disk, that is,
how you implement GenerateAnalysisXML() method.
There are 2 types of data you should save to disk xml files: 1. Xml data,
2. Xml Schema data.
We normally save the Xml data into the disk without any problem, however,
without the Xml schema information serialized to disk, the deserilizer does
not know how to parse the data in the xml file, so it always treats it as
string type.
For example, the below code snippet only saves the original DataSet data
into the disk:
private void button1_Click(object sender, System.EventArgs e)
{
DataTable dt=new DataTable();
dt.Columns.Add(new DataColumn("column1", typeof(int)));
dt.Columns.Add(new DataColumn("column2", typeof(string)));
for(int i=0;i<5;i++)
{
DataRow dr=dt.NewRow();
dr["column1"]=i;
dr["column2"]="item"+i.ToString();
dt.Rows.Add(dr);
}
DataSet ds=new DataSet();
ds.Tables.Add(dt);
MessageBox.Show(ds.Tables[0].Columns[0].DataType.ToString());
ds.WriteXml("F:\\test.xml");
}
When reading the xml file, DataSet.ReadXml method does not know how to
parse the integer data in the column1, so it will always parse it as a
string. So after reading it in DataSet, we will find the column1's datatype
is string:
private void button2_Click(object sender, System.EventArgs e)
{
DataSet ds=new DataSet();
ds.ReadXml("F:\\test.xml");
MessageBox.Show(ds.Tables[0].Columns[0].DataType.ToString());
}
To get the correct type, you should serialize the schema information and
read the schema before reading data, like this:
private void button1_Click(object sender, System.EventArgs e)
{
DataTable dt=new DataTable();
dt.Columns.Add(new DataColumn("column1", typeof(int)));
dt.Columns.Add(new DataColumn("column2", typeof(string)));
for(int i=0;i<5;i++)
{
DataRow dr=dt.NewRow();
dr["column1"]=i;
dr["column2"]="item"+i.ToString();
dt.Rows.Add(dr);
}
DataSet ds=new DataSet();
ds.Tables.Add(dt);
MessageBox.Show(ds.Tables[0].Columns[0].DataType.ToString());
ds.WriteXml("F:\\test.xml");
ds.WriteXmlSchema("F:\\testSchema.xml");
}
private void button2_Click(object sender, System.EventArgs e)
{
DataSet ds=new DataSet();
ds.ReadXmlSchema("F:\\testSchema.xml");
ds.ReadXml("F:\\test.xml");
MessageBox.Show(ds.Tables[0].Columns[0].DataType.ToString());
}
As you will see that, the column1's datatype will be correctly parse as
integer now.
So the key point is that you have to correctly generate the schema xml file
for the xml data.
If you failed to generate the xml schema inforamtion but still want to get
the correct data type in the reading, your reading code must know of the
schema information advance, so that you can manually construct the
DataTable/DataColumn schema type information by code before reading data,
like this:
private void button1_Click(object sender, System.EventArgs e)
{
DataTable dt=new DataTable();
dt.Columns.Add(new DataColumn("column1", typeof(int)));
dt.Columns.Add(new DataColumn("column2", typeof(string)));
for(int i=0;i<5;i++)
{
DataRow dr=dt.NewRow();
dr["column1"]=i;
dr["column2"]="item"+i.ToString();
dt.Rows.Add(dr);
}
DataSet ds=new DataSet();
ds.Tables.Add(dt);
MessageBox.Show(ds.Tables[0].Columns[0].DataType.ToString());
ds.WriteXml("F:\\test.xml");
}
private void button2_Click(object sender, System.EventArgs e)
{
DataSet ds=new DataSet();
//construct the schema types manually with code
DataTable dt=new DataTable();
dt.Columns.Add(new DataColumn("column1", typeof(int)));
dt.Columns.Add(new DataColumn("column2", typeof(string)));
ds.Tables.Add(dt);
ds.ReadXml("F:\\test.xml");
MessageBox.Show(ds.Tables[0].Columns[0].DataType.ToString());
}
Once you construct the columns datatype before ReadXml, the data in xml
file will be parsed correctly to integer type.
If you still have anything unclear, or need any help, please feel free to
tell me, thanks.
Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.