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:
43:
44:
45:
46:
47:
|
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
namespace XMLParser
{
class MainClass
{
public static void Main (string[] args)
{
Console.WriteLine (string.Join ("\n", ParseLibrary ("beispiel.xml")));
}
public static string[] ParseManual(string filename) {
List<string> texts = new List<string> ();
string xml = File.ReadAllText (filename);
while(xml.IndexOf("<balloon who=\"") > 0) {
xml = xml.Substring (xml.IndexOf ("<balloon who=\"") + 14);
texts.Add (xml.Substring (0, xml.IndexOf ("\"")));
xml = xml.Substring (xml.IndexOf ("<p>") + 3);
texts.Add (" " + xml.Substring (0, xml.IndexOf ("</p>")));
}
return texts.ToArray ();
}
public static string[] ParseLibrary(string filename) {
XElement root = XElement.Load (filename);
List<string> entries = new List<string> ();
ParseLibraryRecurs (root, entries);
return entries.ToArray ();
}
private static void ParseLibraryRecurs(XElement node, List<string> list) {
if (node.Name.LocalName == "balloon" && node.Attribute ("who") != null) {
list.Add (node.Attribute ("who").Value);
}
if (node.Name.LocalName == "p") {
list.Add (" " + node.Value);
}
foreach (XElement elem in node.Elements()) {
ParseLibraryRecurs (elem, list);
}
}
}
} |