简体   繁体   English

C# - 如何从xaml resourcedictionary格式的字符串中获取密钥和值?

[英]C# - How can I get the key and value from strings of xaml resourcedictionary format?

I read the xaml file as binaryreader and have it as a string. 我将xaml文件作为binaryreader读取并将其作为字符串。

And, 和,

<System: String x: Key = "ABC"> AAAA </ System: String>
.........................many
<System: String x: Key = "ZZZ"> ASDQWE </ System: String>

I want to get ABC and AAAA as a String. 我想把ABC和AAAA作为一个字符串。

Whether there is a parser or method that can have this type of list ?? 是否有一个解析器或方法可以有这种类型的列表?

This is my xaml file to string code 这是我的xaml文件到字符串代码

BinaryReader b = new BinaryReader(file.InputStream);
byte[] binData = b.ReadBytes((int)file.InputStream.Length);
string result = System.Text.Encoding.UTF8.GetString(binData);

Xaml file is a xml file. Xaml文件是一个xml文件。 So you can easily use LinqToXml to select the nodes and values. 因此,您可以轻松使用LinqToXml来选择节点和值。

I read the file line by line and extracted it using regular expressions. 我逐行读取文件并使用正则表达式提取它。

    StreamReader reader = new StreamReader(file.InputStream);
    string textLine = reader.ReadLine();
    string key = GetKeyStringFromXaml(textLine);
    string val = GetValStringFromXaml(textLine);
    public string GetKeyStringFromXaml(string textLine) {
        Regex regex = new Regex("<System:String x:Key=\"(.*)\">(.*)</System:String>");
        var v = regex.Match(textLine);
        return v.Groups[1].ToString();
    }
    public string GetValStringFromXaml(string textLine) {
        Regex regex = new Regex("<System:String x:Key=\"(.*)\">(.*)</System:String>");
        var v = regex.Match(textLine);
        return v.Groups[2].ToString();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM