C# XML到DataTable-返回空白单元格

C# XML到DataTable-返回空白单元格,c#,sql,xml,C#,Sql,Xml,这将是一个漫长的问题-我花了一整天的时间试图解决这个问题,所以也许你们可以帮忙!我试图通过允许数据库结构在出现新结构(XML格式)时进行更改来证明我的数据库应用程序的未来性 目前,我正设法将数据库结构导出为XML。见下面的工作代码: public void generateXMLStructureCompactDB(string fileName) { DataTable table = new DataTable(); int position;

这将是一个漫长的问题-我花了一整天的时间试图解决这个问题,所以也许你们可以帮忙!我试图通过允许数据库结构在出现新结构(XML格式)时进行更改来证明我的数据库应用程序的未来性

目前,我正设法将数据库结构导出为XML。见下面的工作代码:

    public void generateXMLStructureCompactDB(string fileName)
    {
        DataTable table = new DataTable();
        int position;
        FileStream fsWrite;
        StreamWriter sw;

        string version = "1.1.1.0";
        table.Columns.Add("TableName", Type.GetType("System.String"));
        table.Columns.Add("ColName", Type.GetType("System.String"));
        table.Columns.Add("Position", Type.GetType("System.Int32"));
        table.Columns.Add("DataType", Type.GetType("System.String"));
        table.Columns.Add("MaximumLength", Type.GetType("System.Int32"));
        table.Columns.Add("Precision", Type.GetType("System.Int32"));
        table.Columns.Add("Scale", Type.GetType("System.Int32"));
        table.Columns.Add("Nullable", Type.GetType("System.Boolean"));
        table.Columns.Add("Identity", Type.GetType("System.Boolean"));
        table.Columns.Add("IdentitySeed", Type.GetType("System.Int32"));
        table.Columns.Add("IdentityIncrement", Type.GetType("System.Int32"));

        string sql = "select Table_Name, Column_Name, Ordinal_Position, Data_Type, " +
            "Character_Maximum_Length, Numeric_Precision, Numeric_Scale, Is_Nullable, " +
            "case when AutoInc_Seed is null then 0 else 1 end as IS_IDENTITY, " +
            "AutoInc_Seed, AutoInc_Increment from information_schema.columns";
        runSQL(sql, out table);

        if (File.Exists(fileName))
            File.Delete(fileName);

        fsWrite = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
        sw = new StreamWriter(fsWrite, Encoding.ASCII);

        XmlWriter writer = new XmlTextWriter(sw);
        writer.WriteStartDocument(false);
        writer.WriteRaw(Environment.NewLine);
        writer.WriteComment("DBVersion=" + DBVersion + ", created=" + DateTime.Now.ToString());
        writer.WriteRaw(Environment.NewLine);

        writer.Close();
        sw.Close();
        fsWrite.Close();

        fsWrite = new FileStream(fileName, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
        sw = new StreamWriter(fsWrite, Encoding.ASCII);

        DataTable indexes = new DataTable();
        indexes.Columns.Add("TableName", Type.GetType("System.String"));
        indexes.Columns.Add("Schema", Type.GetType("System.String"));
        indexes.Columns.Add("IndexName", Type.GetType("System.String"));
        indexes.Columns.Add("Clustered", Type.GetType("System.Boolean"));
        indexes.Columns.Add("Unique", Type.GetType("System.Boolean"));
        indexes.Columns.Add("IndexColName", Type.GetType("System.String"));
        sql = "select Table_Name, Table_Schema, Index_Name, [Clustered], [Unique], " + 
            "Column_Name from information_schema.indexes";
        runSQL(sql, out indexes);

        table.TableName = "Tables";
        indexes.TableName = "Indexes";

        table.WriteXml(sw);
        sw.WriteLine("");
        indexes.WriteXml(sw);
        sw.Close();
        fsWrite.Close();
    }
    static bool VerifyStructure()
    {
        FileStream fsWrite;
        int i;
        StreamWriter sw;
        string DBStructureVersion = "0.0.0.0";
        string xmlFile;
        string s;
        string tmp = Environment.GetEnvironmentVariable("TEMP");
        if (tmp == "")
            tmp = Environment.GetEnvironmentVariable("SystemDrive");
        try
        {
            xmlFile = tmp + @"\dbStructure.xml";
            if (File.Exists(xmlFile))
                File.Delete(xmlFile);

            fsWrite = new FileStream(xmlFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
            sw = new StreamWriter(fsWrite, Encoding.ASCII);
            sw.Write(syntos.Properties.Resources.dbStructure);
            sw.Close();
            fsWrite.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error reading structure xml file: " + ex.Message);
            return false;
        }
        if (!File.Exists(xmlFile))
        {
            MessageBox.Show("XML Structure file '" + xmlFile + "' does not exist.");
            return false;
        }
        StreamReader sr = new StreamReader(xmlFile);
        s = sr.ReadLine();
        while (s != null)
        {
            if (s.Contains("DBVersion="))
            {
                i = s.IndexOf("DBVersion=");
                DBStructureVersion = s.Substring(i + 10);
                i = DBStructureVersion.IndexOf(",");
                DBStructureVersion = DBStructureVersion.Substring(0, i);
                break;
            }
            s = sr.ReadLine();
        }
        sr.Close();

        // Split the remaining XML file into two: table datatable file and index datatable file
        // Write out to file1 all the table information and to file2 all the index information
        string file1path;
        string file2path;

        try
        {
            file1path = tmp + @"\tables.xml";
            file2path = tmp + @"\indexes.xml";
            if (File.Exists(file1path))
                File.Delete(file1path);

            if (File.Exists(file2path))
                File.Delete(file2path);

            FileStream fsw1 = new FileStream(file1path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
            FileStream fsw2 = new FileStream(file2path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
            StreamWriter sw1 = new StreamWriter(fsw1, Encoding.ASCII);
            StreamWriter sw2 = new StreamWriter(fsw2, Encoding.ASCII);

            // Create a file containing just the table / column definitions
            sr = new StreamReader(xmlFile);
            s = sr.ReadToEnd();
            sr.Close();

            i = s.IndexOf("<DocumentElement>");
            s = s.Substring(i);
            i = s.IndexOf("</DocumentElement>");
            s = s.Substring(0, i + 18);
            sw1.Write(s);
            sw1.Close();
            fsw1.Close();

            // Create a file containing the index column definitions
            sr = new StreamReader(xmlFile);
            s = sr.ReadToEnd();
            sr.Close();

            i = s.IndexOf("</DocumentElement>");
            s = s.Substring(i + 18);
            sw2.Write(s);
            sw2.Close();
            fsw2.Close();
            sr.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error producing the table and index XML file. " + ex.Message);
            return false;
        }

        DataTable table = new DataTable();
        table.Columns.Add("TableName", Type.GetType("System.String"));
        table.Columns.Add("ColName", Type.GetType("System.String"));
        table.Columns.Add("Position", Type.GetType("System.Int32"));
        table.Columns.Add("DataType", Type.GetType("System.String"));
        table.Columns.Add("MaximumLength", Type.GetType("System.Int32"));
        table.Columns.Add("Precision", Type.GetType("System.Int32"));
        table.Columns.Add("Scale", Type.GetType("System.Int32"));
        table.Columns.Add("Nullable", Type.GetType("System.Boolean"));
        table.Columns.Add("Identity", Type.GetType("System.Boolean"));
        table.Columns.Add("IdentitySeed", Type.GetType("System.Int32"));
        table.Columns.Add("IdentityIncrement", Type.GetType("System.Int32"));
        table.TableName = "Tables";

        DataTable indexes = new DataTable();
        indexes.Columns.Add("TableName", Type.GetType("System.String"));
        indexes.Columns.Add("Schema", Type.GetType("System.String"));
        indexes.Columns.Add("IndexName", Type.GetType("System.String"));
        indexes.Columns.Add("Clustered", Type.GetType("System.Boolean"));
        indexes.Columns.Add("Unique", Type.GetType("System.Boolean"));
        indexes.Columns.Add("IndexColName", Type.GetType("System.String"));
        indexes.TableName = "Indexes";

        // Read in the XML for the table / columns
        sr = new StreamReader(file1path);
        table.ReadXml(sr);
        sr.Close();
        if (table.Rows.Count == 0)
        {
            MessageBox.Show("No loadable table rows found in XML file");
            sw.Close();
            fsWrite.Close();
            return false;
        }
        sr = new StreamReader(file2path);
        indexes.ReadXml(sr);
        sr.Close();
        if (indexes.Rows.Count == 0)
        {
            MessageBox.Show("No loadable index rows found in XML file");
            sw.Close();
            fsWrite.Close();
            return false;
        }
        DbFunctions.BackupDatabase();
        bool success1 = CreateTempTables(table);
        return true;
    }
下面是我生成的XML文件(实际版本要大得多,但我已将其拆分为一个表和一个索引):

通过使用断点和调试语句,我可以看到表中填充了正确数量的行和列,但是所有这些都是空的。只有空的牢房。我曾经使用
table.Rows.Count
查看此表中是否实际有行

如果有人能看到我在这里的立场是否正确,我将永远感激!提前感谢:)

编辑: 我已经用另一个XML表测试了这一点,它是有效的。我生成XML文件的方式一定有问题,但我看不出有什么问题

一旦这个问题允许,我会尽快给它加上一笔赏金;)


问题摘要:当系统生成的XML文件(第一个代码块)再次导入程序(第三个代码块)时,行和列都存在,但所有单元格都为空。

列名不匹配-名称区分大小写。 您可以在空表上导入
XML
,该方法将为您创建列

    static bool VerifyStructure()
    {
        FileStream fsWrite;
        int i;
        StreamWriter sw;
        string DBStructureVersion = "0.0.0.0";
        string xmlFile;
        string s;
        string tmp = Environment.GetEnvironmentVariable("TEMP");
        if (tmp == "")
            tmp = Environment.GetEnvironmentVariable("SystemDrive");
        try
        {
            xmlFile = tmp + @"\dbStructure.xml";
            if (File.Exists(xmlFile))
                File.Delete(xmlFile);

            fsWrite = new FileStream(xmlFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
            sw = new StreamWriter(fsWrite, Encoding.ASCII);
            sw.Write(syntos.Properties.Resources.dbStructure);
            sw.Close();
            fsWrite.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error reading structure xml file: " + ex.Message);
            return false;
        }
        if (!File.Exists(xmlFile))
        {
            MessageBox.Show("XML Structure file '" + xmlFile + "' does not exist.");
            return false;
        }
        StreamReader sr = new StreamReader(xmlFile);
        s = sr.ReadLine();
        while (s != null)
        {
            if (s.Contains("DBVersion="))
            {
                i = s.IndexOf("DBVersion=");
                DBStructureVersion = s.Substring(i + 10);
                i = DBStructureVersion.IndexOf(",");
                DBStructureVersion = DBStructureVersion.Substring(0, i);
                break;
            }
            s = sr.ReadLine();
        }
        sr.Close();

        // Split the remaining XML file into two: table datatable file and index datatable file
        // Write out to file1 all the table information and to file2 all the index information
        string file1path;
        string file2path;

        try
        {
            file1path = tmp + @"\tables.xml";
            file2path = tmp + @"\indexes.xml";
            if (File.Exists(file1path))
                File.Delete(file1path);

            if (File.Exists(file2path))
                File.Delete(file2path);

            FileStream fsw1 = new FileStream(file1path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
            FileStream fsw2 = new FileStream(file2path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
            StreamWriter sw1 = new StreamWriter(fsw1, Encoding.ASCII);
            StreamWriter sw2 = new StreamWriter(fsw2, Encoding.ASCII);

            // Create a file containing just the table / column definitions
            sr = new StreamReader(xmlFile);
            s = sr.ReadToEnd();
            sr.Close();

            i = s.IndexOf("<DocumentElement>");
            s = s.Substring(i);
            i = s.IndexOf("</DocumentElement>");
            s = s.Substring(0, i + 18);
            sw1.Write(s);
            sw1.Close();
            fsw1.Close();

            // Create a file containing the index column definitions
            sr = new StreamReader(xmlFile);
            s = sr.ReadToEnd();
            sr.Close();

            i = s.IndexOf("</DocumentElement>");
            s = s.Substring(i + 18);
            sw2.Write(s);
            sw2.Close();
            fsw2.Close();
            sr.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error producing the table and index XML file. " + ex.Message);
            return false;
        }

        DataTable table = new DataTable();
        table.Columns.Add("TableName", Type.GetType("System.String"));
        table.Columns.Add("ColName", Type.GetType("System.String"));
        table.Columns.Add("Position", Type.GetType("System.Int32"));
        table.Columns.Add("DataType", Type.GetType("System.String"));
        table.Columns.Add("MaximumLength", Type.GetType("System.Int32"));
        table.Columns.Add("Precision", Type.GetType("System.Int32"));
        table.Columns.Add("Scale", Type.GetType("System.Int32"));
        table.Columns.Add("Nullable", Type.GetType("System.Boolean"));
        table.Columns.Add("Identity", Type.GetType("System.Boolean"));
        table.Columns.Add("IdentitySeed", Type.GetType("System.Int32"));
        table.Columns.Add("IdentityIncrement", Type.GetType("System.Int32"));
        table.TableName = "Tables";

        DataTable indexes = new DataTable();
        indexes.Columns.Add("TableName", Type.GetType("System.String"));
        indexes.Columns.Add("Schema", Type.GetType("System.String"));
        indexes.Columns.Add("IndexName", Type.GetType("System.String"));
        indexes.Columns.Add("Clustered", Type.GetType("System.Boolean"));
        indexes.Columns.Add("Unique", Type.GetType("System.Boolean"));
        indexes.Columns.Add("IndexColName", Type.GetType("System.String"));
        indexes.TableName = "Indexes";

        // Read in the XML for the table / columns
        sr = new StreamReader(file1path);
        table.ReadXml(sr);
        sr.Close();
        if (table.Rows.Count == 0)
        {
            MessageBox.Show("No loadable table rows found in XML file");
            sw.Close();
            fsWrite.Close();
            return false;
        }
        sr = new StreamReader(file2path);
        indexes.ReadXml(sr);
        sr.Close();
        if (indexes.Rows.Count == 0)
        {
            MessageBox.Show("No loadable index rows found in XML file");
            sw.Close();
            fsWrite.Close();
            return false;
        }
        DbFunctions.BackupDatabase();
        bool success1 = CreateTempTables(table);
        return true;
    }