如何将.txt文件中的数据转换为xml? c#.[英] How would I convert data in a .txt file into xml? c#

本文是小编为大家收集整理的关于如何将.txt文件中的数据转换为xml? c#.的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我在文本文件中有成千上万的数据,我想通过将其转换为更容易搜索的内容(我希望XML或其他类型的大数据结构,尽管我不确定它是否会最好是我想到的).

每行的数据看起来像这样:

书31,托马斯,乔治,32,34,154

(每本书不是唯一的,它们都是索引,因此书籍将在其中列出了几个不同的条目,而且数字是它们列出的页面)

)

所以我有点迷失方面的方法,我想读取.txt文件,修剪所有空格和逗号,我基本上会得到如何为它做好准备,但是我该如何编程地制作XML中的许多元素和值或填充了其他一些大数据结构?

推荐答案

如果您的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);
    }
}

然后,您可以轻松地搜索allbooks列表,例如linq.

编辑

现在,您已经澄清了您的意见,我对解析进行了一些调整以更好地满足您的需求.

如果要更轻松地搜索title或person的书籍列表,也可以在每个属性上创建一个查找

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

so personLookup["Thomas, George"]将为您提供所有提及"托马斯,乔治"和titleLookup["Book 31"]的书籍列表. p>

其他推荐答案

如果您希望CSV文件通过将其转换为更容易搜索的内容来易于搜索,则可以将其转换为DataTable.

如果您想要数据,则可以将Linq到XML进行搜索

以下类同时生成DataTable或XML数据格式.您可以通过分隔线,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();
    }
}

要使用的示例:

 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>

 */

本文地址:https://www.itbaoku.cn/post/597321.html

问题描述

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).

The data looks like this for each line:

Book 31, Thomas,George, 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?

推荐答案

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

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.

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

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.

其他推荐答案

If you want the CSV file to make easily searchable by turning it into something easier to search, you can convert it to DataTable.

if you want data , you can use LINQ to XML to search

The following class generates both DataTable or Xml data format. You can pass delimeter ,includeHeader or use the default:

 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>

 */