字符串转换为模拟 [英] Convert string to model

查看:110
本文介绍了字符串转换为模拟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有这个对象:

Let's say I have this object:

public struct Line
{
    public string Name { get; set; }
    public int Value { get; set; }
    public string Alias { get; set; }
}

和我有以下这种语法行的文件:

And I have a file with lines following this syntax:

garbagedata|moregarbagedata|Name|garbagedata3|Value|garbagedatamaximums|Alias\n

注意moregarbagedata [X]可以或可以不存在。正则表达式是需要提取组值。

Note that moregarbagedata[x] may or may not exist. A Regex is needed to extract the group-values.

什么是把这个文件的行到对象的集合,最简单有效的方法是什么?这个集合的顺序并不重要。

What is easiest and most efficient way to turn this file's lines into a collection of Line objects? Order of this collection does not matter.

推荐答案

在我看来,有几个问题需要被问这里。

In my opinion, there are a couple of questions that needs to be asked here.


  1. 您可以更改文件格式?

  2. 有没有办法,你可以指定键/值对一个独立的分隔符?
    {值:垃圾,别名:someotherGarbage},{值:secondGarbage ....}

  3. 是线(Environment.NewLine)的唯一途径包分开吗?

如果答案1为真,
那么你可以使用的XmlSerializer和序列化和反序列化你的对象来来回回。

If the answer to 1 is true, then you can use XmlSerializer and Serialize and Deserialize your objects to and fro.

如果2的答案是真的,
那么你可以看看手动解析文件使用正则表达式或逐行读取线只是寻找价值:然后假定下一个项目是值

If the answer for 2 is true, then you could look at parsing the file manually using regex or reading line by line and just looking for Value: and then assume that the next item is the value of "Value"

如果为3的答案为真,
可能是同样的答案为2。

if the answer for 3 is true, Possibly the same answer as 2.

我希望这有助于。

这篇关于字符串转换为模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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