【问题标题】:Create XML from multiple CSV's in multiple folders从多个文件夹中的多个 CSV 创建 XML
【发布时间】:2015-03-25 16:54:43
【问题描述】:

我所做的重点是我需要从位于一个主文件夹内多个子文件夹中的多个 CSV 文件创建一个主 XML。

这是我现在正在使用的,但它似乎只为最后一个 CSV 覆盖并创建了一个 xml...

String AudioDir = @"C:\XMLFILES";
        DirectoryInfo AudiodirInfo = new DirectoryInfo(AudioDir);
        if (AudiodirInfo.Exists == false)
        {
            Directory.CreateDirectory(AudioDir);
        }

        List<String> AudioXMLFiles = Directory.GetFiles(@"C:\LOGGER AUDIO", "*.csv*", SearchOption.AllDirectories).ToList();
        XElement audxml = null;
        foreach (string file in AudioXMLFiles)
        {
            string[] lines2 = File.ReadAllLines(file);
           audxml = new XElement("root",
           from str in lines2
           let columns = str.Split(',')
           select new XElement("recording_info",
                   new XElement("recorded_accound_id", columns[1]),
                   new XElement("date_created_ts", String.Format("{0:####-##-##  ##:##:##}", Convert.ToInt64(columns[2] + columns[3]))),                                    
                   new XElement("recorded_cid", columns[9]),//1
                   new XElement("recording_tag", columns[1]),
                   new XElement("filename", columns[1] + "_" + columns[2] + "_" + columns[3]),
                   new XElement("record", columns[3]),//Record in TimeoutException format
                   new XElement("from_caller_id", columns[10] + "  <" + columns[8] + ">")



               ));


        }
        audxml.Save(@"C:\XMLFile.xml");

【问题讨论】:

    标签: c# xml csv xelement


    【解决方案1】:

    您在 foreach 的每次迭代中都覆盖了 audxml。您可能想要的是在循环外创建一个根节点,然后将每个文件的 xml 输出添加到该根节点。

    XElement audxml = new XElement("root");
    
    foreach (string file in AudioXMLFiles)
        {
            string[] lines2 = File.ReadAllLines(file);
           XElement filexml = new XElement("root",
           from str in lines2
           let columns = str.Split(',')
           select new XElement("recording_info",
                   new XElement("recorded_accound_id", columns[1]),
                   new XElement("date_created_ts", String.Format("{0:####-##-##  ##:##:##}", Convert.ToInt64(columns[2] + columns[3]))),                                    
                   new XElement("recorded_cid", columns[9]),//1
                   new XElement("recording_tag", columns[1]),
                   new XElement("filename", columns[1] + "_" + columns[2] + "_" + columns[3]),
                   new XElement("record", columns[3]),//Record in TimeoutException format
                   new XElement("from_caller_id", columns[10] + "  <" + columns[8] + ">")
               ));
    
               audXml.Add(fileXml);
        }
        audxml.Save(@"C:\XMLFile.xml");
    

    【讨论】:

      【解决方案2】:

      问题是您在 foreach 循环的每次迭代中都在重新创建文档。

      相反,您应该在循环之前创建根文档。然后将元素添加到循环内的根。

      例子:

      XElement audxml = new XElement("root");
      foreach(...)
      {
        //..
        audxml.Add(new XElement("blabla"));
      }
      

      这会将每次迭代的数据保留在 XElement 中,并且只添加到现有的根元素中。

      // 迈克

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-11-24
        • 2018-05-09
        • 1970-01-01
        • 2011-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多