解析问题键返回“" [英] parsing question keys return ""

查看:66
本文介绍了解析问题键返回“"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,谢谢你在这里阅读我的帖子是我试图解析文件得到左边的每个项目的问题 


是关键,我想要要显示左侧的值并编辑值并保存回来,这是我目前的代码 


私有String头; 
private Dictionary< String,String>令牌;
private String file = Properties.Settings.Default.path_location;
public TextBox elf_naparm_zombie_developer;


public harrs_gsh_editor(string file)// TextBox elf_naparm_zombie_developer
{
Properties.Settings.Default.path_location = file;
Properties.Settings.Default.Save();
loadweapon();
// this.elf_naparm_zombie_developer = elf_naparm_zombie_developer;
}


public void loadweapon()
{

String plainText;
using(StreamReader sr = new StreamReader(this.file))
{
plainText = sr.ReadToEnd();
}

String [] tokens = plainText.Split('');
this.tokens = new Dictionary< string,string>(tokens.Length + 10);

header = tokens [0];
for(int i = 1; i< tokens.Length; i + = 2)
{
String key = tokens [i];
String val =(i + 1< = tokens.Length)?代币[i + 1]:"" ;;

this.tokens.Add(key,val);

}
}



公共字符串搜索(字符串名称)
{
foreach(KeyValuePair<字符串,字符串>令牌中的对)
{
if(pair.Key == name)
return pair.Value;
}

返回null;
}

public void Set(String key,String val)
{
tokens [key] = val;
}

public void保存(字符串文件)
{
使用(StreamWriter sw = new StreamWriter(File.OpenWrite(file)))
{
sw.Write(header);
foreach(KeyValuePair<字符串,字符串>令牌中的对)
{
sw.Write("" + pair.Key +"" + pair.Value);
}
}
}

public void保存()
{
this.Save(file);
}

这是我正在努力工作的文件 





结束文件 




我遇到的问题是当它通过文件填充它时就像"""然后错误作为键不能像我想要的只是 


#define NAPALM_ZOMBIE_TIME_BEFORE_EXPLODE_POSSIBLE<<< ;钥匙&NBSP;   2000<<<价值 



所以使用#define,其余的作为键,然后显示值的文本框,我认为我已将其编码为正确对于那部分它只是得到它的键值对不起作用,我不知道如何解决这个任何帮助将非常感谢
提前感谢elfenliedtopfan5


解决方案

< span style ="font-size:medium">尝试修改后的函数:


使用 System.Xml.Linq;


using System.Xml.XPath;


。 。 。


public
void loadweapon()


{


   
.tnkens =


        ;&NBSP;
文件


           
.ReadLines(
this 。file)


     &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
.Select(line => Regex.Match(line,
@"(?+)?^ \s *#define\s +(。?\s +(+))((\s * //)|


< blockquote>)"
))


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
.Where(m => m.Success&& m.Groups [3] .Success)


   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
.ToDictionary(m => m.Groups [1] .Value,m => m.Groups [3] .Value);


}


hello thank you for reading my post here is the issue im having im trying to parse the file getting each item on the left side 

to be key and i want to display the value on the left and edit the values and save back out this is the code i currently have 

 private String header;
        private Dictionary<String, String> tokens;
        private String file = Properties.Settings.Default.path_location;
        public TextBox elf_naparm_zombie_developer;


        public harrs_gsh_editor(string file)//TextBox elf_naparm_zombie_developer
        {
            Properties.Settings.Default.path_location = file;
            Properties.Settings.Default.Save();
            loadweapon();
           // this.elf_naparm_zombie_developer = elf_naparm_zombie_developer;
        }


        public void loadweapon()
        {

            String plainText;
            using (StreamReader sr = new StreamReader(this.file))
            {
                plainText = sr.ReadToEnd();
            }

            String[] tokens = plainText.Split(' ');
            this.tokens = new Dictionary<string, string>(tokens.Length + 10);

            header = tokens[0];
            for (int i = 1; i < tokens.Length; i += 2)
            {
                String key = tokens[i];
                String val = (i + 1 <= tokens.Length) ? tokens[i + 1] : "";

                this.tokens.Add(key, val);

            }
        }



        public String Search(String name)
        {
            foreach (KeyValuePair<String, String> pair in tokens)
            {
                if (pair.Key == name)
                    return pair.Value;
            }

            return null;
        }

        public void Set(String key, String val)
        {
            tokens[key] = val;
        }

        public void Save(String file)
        {
            using (StreamWriter sw = new StreamWriter(File.OpenWrite(file)))
            {
                sw.Write(header);
                foreach (KeyValuePair<String, String> pair in tokens)
                {
                    sw.Write(" " + pair.Key + " " + pair.Value);
                }
            }
        }

        public void Save()
        {
            this.Save(file);
        }

this is the file i am trying to get working 

end of file 

the issue i have is when it goes though the file it just fills it up like "" and then errors as keys cant be the same how i want is just 

#define NAPALM_ZOMBIE_TIME_BEFORE_EXPLODE_POSSIBLE <<< key   2000 <<< value 

so anything with #define and the the rest as key and then the textbox to display value witch i think i have coded it right for that part its just getting it to key value pair is not working and im not sure how to fix this any help would be very much appreciated thanks in advance elfenliedtopfan5

解决方案

Try a modified function:

using System.Xml.Linq;

using System.Xml.XPath;

. . .

public void loadweapon()

{

    this.tokens =

        File

            .ReadLines( this.file )

            .Select( line => Regex.Match( line, @"^\s*#define\s+(.+?)(\s+(.+?))?((\s*//)|


)" ) )

            .Where( m => m.Success && m.Groups[3].Success )

            .ToDictionary( m => m.Groups[1].Value, m => m.Groups[3].Value );

}


这篇关于解析问题键返回“&quot;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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