C# 需要帮助在C中解析文本文件并将其拆分为键值吗#
我正在尝试解析一个具有特定格式的文本文件,并将其拆分为键值,以后可以在外部编辑这些键值,以便将它们重新构建到另一个文本文件中。我尝试这样做的方式对我来说似乎很肮脏,我使用一个流遍历文件,直到我使用while循环到达引号和大括号。它通常会一直工作,直到我到达一个特定的点,我要么偶然发现一个右括号,要么跳过一些完全破坏格式的终止引号。我尝试了一系列不同的改变,但都不奏效 我试图分析的文件具有以下格式: testparse.txtC# 需要帮助在C中解析文本文件并将其拆分为键值吗#,c#,string,parsing,text,stream,C#,String,Parsing,Text,Stream,我正在尝试解析一个具有特定格式的文本文件,并将其拆分为键值,以后可以在外部编辑这些键值,以便将它们重新构建到另一个文本文件中。我尝试这样做的方式对我来说似乎很肮脏,我使用一个流遍历文件,直到我使用while循环到达引号和大括号。它通常会一直工作,直到我到达一个特定的点,我要么偶然发现一个右括号,要么跳过一些完全破坏格式的终止引号。我尝试了一系列不同的改变,但都不奏效 我试图分析的文件具有以下格式: testparse.txt "testparse.txt" { "TestElement"
"testparse.txt"
{
"TestElement"
{
"value1" "0"
"value2" "stuff"
"value3" "morestuff"
"value4" "25"
"value5" "text"
"value6" "21"
}
"TestElement2"
{
"value1" "0"
"value2" "1"
"value3" "2"
"value4" "3"
"value5" "4"
"value6" "5"
}
}
我感兴趣的是将名称和值(“value1”、“0”)打包到KeyValues中,然后将其打包到元素中,然后再打包到文件中。我什么都用字符串
这是我的密码:
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace hudParse
{
class Program
{
static void Main(string[] args)
{
char qt = '\"';
StreamReader sr = new StreamReader("D:/Desktop/testparse.txt");
HudFile file = new HudFile();
string s = "";
Seek(sr,qt);
do
{
s += (char)sr.Read();
}
while(sr.Peek() != qt);
sr.Read();
file.Name = s;
Console.WriteLine("Filename is " + s);
Seek(sr,'{');
//Main loop
do
{
HudElement element = new HudElement();
Seek(sr,qt);
s = "";
do
{
s += (char)sr.Read();
}
while(sr.Peek() != qt);
sr.Read();
element.Name = s;
Console.WriteLine("New Element name is " + s);
Seek(sr,'{');
do
{
s = "";
KeyValue kv = new KeyValue();
Seek(sr,qt);
do
{
s += (char)sr.Read();
}
while(sr.Peek() != qt);
kv.Name = s;
sr.Read();
s = "";
Seek(sr,qt);
do
{
s += (char)sr.Read();
}
while(sr.Peek() != qt);
kv.Value = s;
sr.Read();
element.Add(kv);
Console.WriteLine("KeyValue added to " + element.Name + ": " + kv.ToString());
}
while(sr.Read() != '}');
}
while((sr.Read() != '}') || (sr.Read() != -1));
file.Write();
sr.Close();
Console.WriteLine("Created file " + file.Name);
}
static void Seek(StreamReader sr, char c)
{
while(sr.Read() != c);
}
}
}
KeyValue.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace hudParse
{
class KeyValue
{
string m_Name;
string m_Value;
public string Name
{
get
{
return m_Name;
}
set
{
m_Name = value.Trim();
}
}
public string Value
{
get
{
return m_Value;
}
set
{
m_Value = value.Trim();
}
}
public KeyValue()
{
m_Name = null;
m_Value = null;
}
public KeyValue(string name, string value)
{
m_Name = name;
m_Value = value;
}
public override string ToString()
{
return '\"'+ m_Name + '\"' + '\t' + '\t' + '\"'+ m_Value + '\"';
}
public string GetName()
{
return m_Name;
}
public string GetValue()
{
return m_Value;
}
public bool isNull()
{
if(m_Name == null)
return true;
return false;
}
}
}
我确信有一种更好的方法来完成我所缺少的所有这些,这样我就不会让空格、制表符和换行符打断我的解析。下面是与我的代码关联的其他类
您的数据看起来像一个损坏的JSON,因此我认为解析它的最简单方法是将其转换为有效的JSON,然后使用一些默认工具,如JSON.NET。您的数据应该如下所示
{
"t1": {
"key1":"value1",
"key2":"value2"
},
"t2": {
"key3":"value3",
"key4":"value4"
}
}
以上示例适用于
字典
类型 文本文件看起来像JSON。您可以将其序列化为一个类,然后从中访问值。这些是您正在使用的JSON文件吗?如果是这样,我建议您使用JSON.NET。无论如何,请注意您可以使用一个Dictionary类,该类具有键/值成员;还有一个KVP类可供您使用:这些是用于源引擎内多个内容的资源文件。我要看一看字典课。