简体   繁体   English

如何将.txt文件中的数据转换为xml? C#

[英]How would I convert data in a .txt file into xml? c#

I have thousands of lines of data in a text file I want to make easily searchable by turning it into something easier to search (I am hoping an XML or another type of large data structure, though I am not sure if it will be the best for what I have in mind). 我想通过将文本文件转换成易于搜索的内容来使文本文件中的数千行数据易于搜索(我希望XML或其他类型的大型数据结构,尽管我不确定它是否会是最好的对于我的想法)。

The data looks like this for each line: 每行的数据如下所示:

Book 31, Thomas,George, 32, 34, 154 书籍31,托马斯·乔治,32,34,154

(each book is not unique, they are indexes so book will have several different entries of whom is listed in it, and the numbers are the page they are listed) (每本书不是唯一的,它们是索引,因此本书将在其中列出几个不同的条目,而数字是列出的页面)

So I am kinda of lost on how to do this, I would want to read the .txt file, trim out all the spaces and commas, I basically get how to prep the data for it, but how would I programmatically make that many elements and values in xml or populate some other large data structure? 因此,我对如何执行此操作有些迷失,我想读取.txt文件,删去所有空格和逗号,基本上可以得到准备数据的方法,但是我将如何以编程方式制作这么多元素和xml中的值还是填充其他大型数据结构?

If your csv file does not change too much and the structure is stable, you could simply parse it to a list of objects at startup 如果您的csv文件变化不大且结构稳定,则可以在启动时将其解析为对象列表

private class BookInfo {
    string title {get;set;}
    string person {get;set;}
    List<int> pages {get;set;}
}


private List<BookInfo> allbooks = new List<BookInfo>();

public void parse() {
    var lines = File.ReadAllLines(filename);  //you could also read the file line by line here to avoid reading the complete file into memory
    foreach (var l in lines) {
       var info = l.Split(',').Select(x=>x.Trim()).ToArray();
       var b = new BookInfo {
          title = info[0],
          person = info[1]+", " + info[2],
          pages = info.Skip(3).Select(x=> int.Parse(x)).ToList()
       };
       allbooks.Add(b);
    }
}

Then you can easily search the allbooks list with for instance LINQ. 然后,您可以轻松搜索带有LINQ的allbooks列表。

EDIT 编辑

Now, that you have clarified your input, I adapted the parsing a little bit to better fit your needs. 现在,您已经澄清了输入内容,我对解析进行了一些调整,以更好地满足您的需求。

If you want to search your booklist by either the title or the person more easily, you can also create a lookup on each of the properties 如果要按titleperson更轻松地搜索书目,还可以在每个属性上创建一个查找

var titleLookup = allbooks.ToLookup(x=> x.title);
var personLookup = allbooks.ToLookup(x => x.person);

So personLookup["Thomas, George"] will give you a list of all bookinfos that mention "Thomas, George" and titleLookup["Book 31"] will give you a list of all bookinfos for "Book 31", ie all persons mentioned in that book. 因此personLookup["Thomas, George"]将为您提供所有提及“ Thomas,George titleLookup["Book 31"]的列表,而titleLookup["Book 31"]将为您提供有关“ Book 31”的所有书信息的列表,即所有提及的人在那本书中。

If you want the CSV file to make easily searchable by turning it into something easier to search, you can convert it to DataTable. 如果希望通过将CSV文件转换为易于搜索的内容来使其易于搜索,则可以将其转换为DataTable。

if you want data , you can use LINQ to XML to search 如果需要数据,可以使用LINQ to XML进行搜索

The following class generates both DataTable or Xml data format. 下列类生成DataTable或Xml数据格式。 You can pass delimeter ,includeHeader or use the default: 您可以传递delimeter,includeHeader或使用默认值:

 class CsvUtility
{
    public DataTable Csv2DataTable(string fileName, bool includeHeader = false, char separator = ',')
    {
        IEnumerable<string> reader = File.ReadAllLines(fileName);
        var data = new DataTable("Table");
        var headers = reader.First().Split(separator);
        if (includeHeader)
        {
            foreach (var header in headers)
            {
                data.Columns.Add(header.Trim());
            }
            reader = reader.Skip(1);
        }
        else
        {
            for (int index = 0; index < headers.Length; index++)
            {
                var header = "Field" + index; // headers[index];
                data.Columns.Add(header);
            }
        }

        foreach (var row in reader)
        {
            if (row != null) data.Rows.Add(row.Split(separator));
        }
        return data;
    }
    public string Csv2Xml(string fileName, bool includeHeader = false, char separator = ',')
    {
        var dt = Csv2DataTable(fileName, includeHeader, separator);
        var stream = new StringWriter();
        dt.WriteXml(stream);
        return stream.ToString();
    }
}

example to use: 使用示例:

 CsvUtility csv = new CsvUtility();
        var dt = csv.Csv2DataTable("f1.txt");

        // Search for  string in any column  
        DataRow[] filteredRows = dt.Select("Field1 LIKE '%" + "Thomas" + "%'"); 

        //search in certain field
        var filtered = dt.AsEnumerable().Where(r => r.Field<string>("Field1").Contains("Thomas"));


        //generate xml
        var xml=  csv.Csv2Xml("f1.txt");
        Console.WriteLine(xml);
/*
    output of xml for your sample:
   <DocumentElement>
     <Table>
       <Field0>Book 31</Field0>
       <Field1> Thomas</Field1>
       <Field2>George</Field2>
       <Field3> 32</Field3>
       <Field4> 34</Field4>
       <Field5> 154</Field5>
    </Table>
</DocumentElement>

 */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM