Autor Beitrag
Raven280438
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 99



BeitragVerfasst: Mo 18.07.11 11:10 
Hi,

ich habe eine Webseite, welche im Quellcode ein PHP-Array abbildet:

ausblenden Quelltext
1:
2:
3:
$array["0"]["name"] = "Username";
$array["0"]["pass"] = "Password";
$array["0"]["path"] = "/path";


Jetzt such ich eine einfache Möglichkeit, diesen Text in ein C# Dictionary umzuwandeln.
Hat jemand einen Ansatzpunkt für mich?


Gruß
Trashkid2000
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 561
Erhaltene Danke: 137



BeitragVerfasst: Mo 18.07.11 20:08 
Hallo,

naja, Du könntest die Zeilen einfach einlesen, eine Regex durchjagen und dann die Werte in ein Dictionary schreiben.
Der reg. Ausdruck könnte wie folgt aussehen:
ausblenden C#-Quelltext
1:
regExpr = new Regex(@"[^\]]*\]\[""(?<Key>[^""]*)""\][^""]*""(?<Value>[^""]*)");					
LG
Raven280438 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 99



BeitragVerfasst: Di 19.07.11 09:38 
Hi,

das mit dem Regex war ne gute Idee.
Ich hab mir allerdings n eigenes Regex geschrieben, da ich ja die ID auch benötige.
Hier der Code, falls jemand das gleiche Problem hat:

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
                WebClient wClient = new WebClient();
                wClient.Encoding = Encoding.UTF8;
                String strSource = wClient.DownloadString(url);

                String[] Lines = strSource.Split('\n');

                List<Dictionary<String, String>> ListReturn = new List<Dictionary<string,string>>();

                System.Text.RegularExpressions.Regex regExpr = new System.Text.RegularExpressions.Regex("^\\$array\\[\"([0-9].*)\"\\]\\[\"([0-9a-zA-Z_\\-].*)\"\\] = \"(.*)\";$");

                foreach (String Line in Lines)
                {
                    if (regExpr.IsMatch(Line))
                    {
                        System.Text.RegularExpressions.Match match = regExpr.Match(Line);

                        System.Text.RegularExpressions.Group g_id = match.Groups[1];
                        System.Text.RegularExpressions.Group g_key = match.Groups[2];
                        System.Text.RegularExpressions.Group g_value = match.Groups[3];
                        System.Text.RegularExpressions.CaptureCollection cc_id = g_id.Captures;
                        System.Text.RegularExpressions.Capture c_id = cc_id[0];
                        System.Text.RegularExpressions.CaptureCollection cc_key = g_key.Captures;
                        System.Text.RegularExpressions.Capture c_key = cc_key[0];
                        System.Text.RegularExpressions.CaptureCollection cc_value = g_value.Captures;
                        System.Text.RegularExpressions.Capture c_value = cc_value[0];

                        int id = Convert.ToInt16(c_id.ToString());
                        String key = c_key.ToString();
                        String value = c_value.ToString();

                        if (ListReturn.Count() > id && ListReturn[id] != null)
                        {
                            ListReturn[id].Add(key, value);
                        }
                        else
                        {
                            Dictionary<String, String> directory = new Dictionary<string,string>();
                            directory.Add(key, value);
                            ListReturn.Add(directory);
                        }
                    }
                }



Gruß