【问题标题】:Need help parsing a text file and splitting it into key values in C#需要帮助解析文本文件并将其拆分为 C# 中的键值
【发布时间】:2016-04-29 07:47:08
【问题描述】:

我正在尝试解析具有特定格式的文本文件并将其拆分为稍后可以在外部编辑的键值,以便将它们重新构建到另一个文本文件中。我尝试这样做的方式对我来说似乎很脏,我正在使用流来遍历文件,直到通过使用 while 循环到达引号和大括号。它主要工作,直到我到达一个特定的点,我偶然发现一个右括号或跳过一些完全搞砸格式的终止引号。我尝试了许多不同的更改,但都无法正常工作。

我尝试解析的文件具有这种格式:

testparse.txt

"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 中,然后将其打包到 Elements 中,然后再打包到 Files 中。我对所有内容都使用字符串。

这是我的代码: 程序.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;
        }          

    }
}

我确信有一种更好的方法来完成我所缺少的所有这些,所以我不会让空格、制表符和换行符破坏我的解析。这是与我的代码相关的其他类。

HudElement.cs HudFile.cs

【问题讨论】:

  • 文本文件看起来像 JSON。您可以将其序列化为一个类,然后从那里访问值。
  • 您正在使用这些 JSON 文件吗?如果是这样,我建议您退出 JSON.NET。无论如何,请注意有一个可以使用的具有键/值成员的 Dictionary 类;还有一个 KVP 课程可供您使用:msdn.microsoft.com/en-us/library/5tbh8a42(v=vs.110).aspx
  • 这些资源文件用于源引擎中的多个事物。我去看看字典类。

标签: c# string parsing text stream


【解决方案1】:

您的数据看起来像一个损坏的 JSON,所以我认为解析它的最简单方法是将其转换为有效的 JSON,然后使用一些默认工具,如 JSON.NET。您的数据应如下所示

{
 "t1": {
   "key1":"value1", 
   "key2":"value2"
  },
 "t2": {
   "key3":"value3", 
   "key4":"value4"
 }
}

以上示例适用于Dictionary<string, Dictionary<string, string>> 类型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多