代码之家  ›  专栏  ›  技术社区  ›  Kpo

在C中没有从我的xmlNodes中得到任何东西#

  •  0
  • Kpo  · 技术社区  · 6 年前

    我已经编写了一个快速代码,尝试从XML文件中获取元素并将它们放入excel表中,但我没有从节点中获取任何内容。我不明白为什么。

        [STAThread]
        static void Main(string[] args)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            XmlDocument xmlDocument = new XmlDocument();
            System.Data.DataTable table = new System.Data.DataTable();
            Microsoft.Office.Interop.Excel.Application excel;
            Microsoft.Office.Interop.Excel.Workbook workBook;
            Microsoft.Office.Interop.Excel.Worksheet workSheet;
            int rowCount = 1;
    
            openFileDialog.Filter = "XML Files|*.xml";
            openFileDialog.Title = "Select the SMS backup";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                xmlDocument.PreserveWhitespace = true;
                try
                {
                    xmlDocument.Load(openFileDialog.FileName);
                }
                catch (System.IO.FileNotFoundException)
                {
                }
                table.Columns.Add("Contact name", typeof(string));
                table.Columns.Add("Date", typeof(DataFormats));
                table.Columns.Add("Message", typeof(string));
                foreach (XmlNode node in xmlDocument.DocumentElement.ChildNodes)
                {
                    System.Diagnostics.Debug.WriteLine(node.InnerText);
                    if (node.InnerText.Contains("contact_name"))
                    {
                        table.Rows.Add(
                            node.Attributes["contact_name"]?.InnerText,
                            node.Attributes["readable_date"]?.InnerText,
                            node.Attributes["body"]?.InnerText
                            );
                    }
                }
                try
                {
                    excel = new Microsoft.Office.Interop.Excel.Application
                    {
                        Visible = true,
                        DisplayAlerts = false
                    };
                    workBook = excel.Workbooks.Add(Type.Missing);
                    workSheet = (Microsoft.Office.Interop.Excel.Worksheet) workBook.ActiveSheet;
                    workSheet.Name = "SMS backup";
                    foreach (System.Data.DataRow dataRow in table.Rows)
                    {
                        rowCount += 1;
                        for (int i = 1; i <= table.Columns.Count; i++)
                        {
                            workSheet.Cells[rowCount, i] = dataRow[i - 1].ToString();
                        }
    
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    workSheet = null;
                    workBook = null;
                }
            }
        }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Caius Jard    6 年前

    继我的评论之后,这里有一些更深入的建议。我是在手机上写的,所以请原谅这不是一个完整的工作代码

    右键单击解决方案资源管理器中的解决方案节点,然后选择“管理解决方案的Nuget软件包”

    安装epplus-它直接创建excel文件(它们实际上只是一个名为xlsx的zip文件中的xml文件),而不需要安装excel interop,这可能是一件麻烦事,最好避免

    将xml文档读入数据集中:

    DataSet da = new DataSet();
    da.ReadXml(path to your file);
    

    ds 然后单击工具提示中显示的放大镜

    熟悉xml文档在数据集中的显示方式—具有子节点的节点往往表示为具有将它们链接在一起的关系的分离表,因此您可能最终会遇到类似xml的问题

    xml
      parentnode1
        childnode1
      parentnode2
        childnode2
    /xml
    

    foreach(var ro in da.Tables["parentnode"].Rows)
      //do stuff with parent rows
    
      //iterate child rows
      foreach(var cro in ro.GetChildRows()){
        //do stuff with child rows
      }
    }
    

    现在看起来您的xml不是这样构造的,但很难说清楚

    因此,您现在将xml文档作为数据集中的一组表,并希望它们作为excel工作表。Epplus可以通过几行代码从数据表(数据集是数据表的集合)创建excel文件,请参阅 Export DataTable to excel with EPPlus

    如果您的数据集只包含一个表,那么工作就差不多结束了。如果它有多个表,我建议您创建一个新的DataTable,然后在数据集上循环,从各种ds.tables[“table name here”]中选取值并填充DataTable

    因此,要实现您的程序,请将这组注释作为代码实现

    //read xml file to DataSet - 2 lines of code
    
    //EITHER xml is flat structure, results in one datatable in the set 
    //so just remove or rename columns as required - few lines
    
    //OR DataSet has 3 tables, make a new datatable and loop over the 3, 
    //populating the new one, to flatten it - few lines
    
    //use epplus to turn the single datatable to an excel sheet - 4 lines
    

    问题解决了,希望用大约10行代码