CSV定界到XML - 文件夹层次结构

时间:2017-08-23 09:13:14

标签: c# xml linq csv doubly-linked-list

这是我第一次发帖,所以我为任何无知或未能使用的例子道歉。

我有一个控制台应用程序项目来创建我给了一些公平的CSV文件,我需要创建一些父/子/孙子关系(XML?也许? - 然后我可以用它来用最少的电话上传和写入DMS - 我不想查询文件夹是否一遍又一遍地存在)

我对这一点有点不了解

我需要知道没有第三方库依赖的最佳方法,纯C#,使用OLEDB JET提供程序很可能是必需的,因为它将处理所需的解析,没有关于CSV文件的命令日期,前几年可能会出现在列表中,反之亦然。

以下是CSV输出的示例

"DESCRIPTION1","8, 5/8\" X 6.4MM","STRING","filename001.pdf","2016-09-19","1"  
"DESCRIPTION2","12, 3/4\" X 6.4MM","STRING","filename001.pdf","2016-09-19","1"  
"DESCRIPTION3","12, 3/4\" X 6.4MM","STRING","filename001.pdf","2016-09-19","1"  
"another description 20# gw","1","388015","Scan123.pdf","2015-10-24","1"  
"another description 20# gw","3","385902","Scan456.pdf","2015-04-14","1"  
"STRINGVAL1","273.10 X 9.27 X 6000","45032-01","KHJDWNEJWKFD9101529.pdf","2012-02-03","1"  
"STRINGVAL2","273.10 X 21.44 X 6000","7-09372","DJSWH68767681540.pdf","2017-02-03","1"  

结束输出将是(YEAR/MONTH/FILENAME +(每个文件的属性 - 这些用于最终更新DMS中的列))

从具有日期

的列中检索的年份和月份

如果YEAR已存在,则不会再次创建 如果该年份下的月份存在,则不会再次创建 如果文件名已经存在于该年/月之下,则不会再次创建该文件名但是该文件名的附加属性将被添加到属性 - "行分隔?"

必需输出:

enter image description here

我尝试过Linq查询开始输出可能需要的XML以便我继续进行,但它输出的每一行都没有分组,我现在不熟悉Linq。

我也遇到了.Split(',')这样做的基本转义问题(请参阅上面的原始CSV示例,与我在我的测试文件和下面的示例中使用TAB分离进行比较)这就是我希望Oledb提供商处理它​​的原因。

string[] source = File.ReadAllLines(@"C:\Processing\In\mockCsv.csv");
XElement item = new XElement("Root",
    from str in source
    let fields = str.Split('\t')
    select new XElement("Year", fields[4].Substring(0, 4),
    new XElement("Month", fields[4].Substring(5, 2),
        new XElement("FileName", fields[3]),
        new XElement("Description",fields[0]),
        new XElement("Length", fields[1]),
        new XElement("Type", fields[2]),
        new XElement("FileName", fields[3]),
        new XElement("Date", fields[4]),
        new XElement("Authorised", fields[5]))
        )                
);

我还需要记录流程的每一步,以便设置Logger类

private class Logger
{
    private static string LogFile = null;

    internal enum MsgType
    {
        Info,
        Debug,
        Error
    }

    static Logger()
    {
        var processingDetails = ConfigurationManager.GetSection(SECTION_PROCESSINGDETAILS) as NameValueCollection;
        LogFile = Path.Combine(processingDetails[KEY_WORKINGFOLDER],
                                String.Format("Log_{0}.txt", StartTime.ToString("MMMyyyy")));
        if (File.Exists(LogFile))
            File.Delete(LogFile);
    }

    internal static void Write(string msg, MsgType msgType, bool isNewLine, bool closeLine)
    {
        if (isNewLine)
            msg = String.Format("{0} - {1} : {2}", DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"), msgType, msg);

        if (closeLine)
            Console.WriteLine(msg);
        else
            Console.Write(msg);

        if (String.IsNullOrEmpty(LogFile))
            return;

        try
        {
            using (StreamWriter sw = new StreamWriter(LogFile, true))
            {
                if (closeLine)
                    sw.WriteLine(msg);
                else
                    sw.Write(msg);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}

用作

Logger.Write(String.Format("Reading records from csv file ({0})... ",
            csvFile), Logger.MsgType.Info, true, false);

1 个答案:

答案 0 :(得分:1)

试试以下内容。如果您正在从文件中读取,请使用StreamReader而不是StringReader:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using System.Text.RegularExpressions;


namespace ConsoleApplication74
{
    class Program
    {

        static void Main(string[] args)
        {
            string input =
                        "\"DESCRIPTION1\",\"8, 5/8 X 6.4MM\",\"STRING\",\"filename001.pdf\",\"2016-09-19\",\"1\"\n" +
                        "\"DESCRIPTION2\",\"12, 3/4 X 6.4MM\",\"STRING\",\"filename001.pdf\",\"2016-09-19\",\"1\"\n" +
                        "\"DESCRIPTION3\",\"12, 3/4 X 6.4MM\",\"STRING\",\"filename001.pdf\",\"2016-09-19\",\"1\"\n" +
                        "\"another description 20# gw\",\"1\",\"388015\",\"Scan123.pdf\",\"2015-10-24\",\"1\"\n" +
                        "\"another description 20# gw\",\"3\",\"385902\",\"Scan456.pdf\",\"2015-04-14\",\"1\"\n" +
                        "\"STRINGVAL1\",\"273.10 X 9.27 X 6000\",\"45032-01\",\"KHJDWNEJWKFD9101529.pdf\",\"2012-02-03\",\"1\"\n" +
                        "\"STRINGVAL2\",\"273.10 X 21.44 X 6000\",\"7-09372\",\"DJSWH68767681540.pdf\",\"2017-02-03\",\"1\"\n";

            string pattern = "\\\"\\s*,\\s*\\\"";

            string inputline = "";
            StringReader reader = new StringReader(input);

            XElement root = new XElement("Root");
            while ((inputline = reader.ReadLine()) != null)
            {
                string[] splitLine = Regex.Split(inputline,pattern);
                Item newItem = new Item() {
                    description = splitLine[0].Replace("\"",""),
                    length = splitLine[1],
                    type = splitLine[2],
                    filename = splitLine[3],
                    date = DateTime.Parse(splitLine[4]),
                    authorized = splitLine[5].Replace("\"", "") == "1" ? true : false
                };

                Item.items.Add(newItem);
            }

            foreach(var year in Item.items.GroupBy(x => x.date.Year).OrderBy(x => x.Key))
            {
                XElement newYear = new XElement("_" + year.Key.ToString());
                root.Add(newYear);
                foreach(var month in year.GroupBy(x => x.date.Month).OrderBy(x => x.Key))
                {
                    XElement newMonth = new XElement("_" + month.Key.ToString());
                    newYear.Add(newMonth);

                    newMonth.Add(
                        month.OrderBy(x => x.date).Select(x => new XElement(
                            x.filename,
                            string.Join("\r\n", new object[] {
                                x.description,
                                x.length,
                                x.type,
                                x.date.ToString(),
                                x.authorized.ToString()
                            }).ToList()
                    )));
                }
            }
        }

    }
    public class Item
    {
        public static List<Item> items = new List<Item>();

        public string description { get; set; }
        public string length { get; set; }
        public string type { get; set; }
        public string filename { get; set; }
        public DateTime date { get; set; }
        public Boolean authorized { get; set; }
    }
}