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

查看:311
本文介绍了我如何将.txt文件中的数据转换为xml? C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个文本文件中有数千行数据我想通过把它变成更容易搜索的东西(我希望一个XML或另一种类型的大数据结构,虽然我不知道是否会



每行的数据如下:


Book 31,Thomas,George,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); //你也可以在这里读取文件行,以避免读取完整的文件到内存
foreach(var l in lines){
var info = l.Split(',')。 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);
}
}

然后,您可以轻松搜索



现在,你可以使用已经澄清了您的输入,我适应了解析一点更好地适合您的需要。



如果您要通过 title code>更容易,你也可以创建查找每个属性

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

因此 personLookup [Thomas,George] 会给你一个提供Thomas,George和 titleLookup [Book 31] 的所有bookinfos的列表将给你一个列表的所有bookinfos ,即该书中提及的所有人。


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.

这篇关于我如何将.txt文件中的数据转换为xml? C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆