1: static void Main(string[] args)
2: {
3: string _Xml = File.ReadAllText(@"C:\Program Files\Microsoft Visual Studio 9.0\VSTSDB\Extensions\SqlServer\2008\DBSchemas\msdb.dbschema");
4: Console.WriteLine("Read {0} characters\n", _Xml.Length);
5:
6: //search using linq-to-xml
7: TestFunction("Linq to Xml", _Xml, SearchLinqToXml);
8:
9: //search using regex
10: TestFunction("Regular Expression", _Xml, SearchRegex);
11:
12: //done
13: Console.WriteLine("\ndone...");
14: Console.ReadLine();
15: }
16:
17: private static void TestFunction(string name, string xmlToSearch, Func<string, string> searchFunction)
18: {
19: Stopwatch _Timer = new Stopwatch();
20: _Timer.Start();
21: Console.WriteLine("Result: {0}", searchFunction(xmlToSearch));
22: _Timer.Stop();
23: Console.WriteLine("{0} response: {1}", name, _Timer.ElapsedMilliseconds);
24: }
25:
26: private static string SearchRegex(string xmlToSearch)
27: {
28: Regex _Regex = new Regex("^.*?Name=\"\\[MSDBLog\\]\".*?Property Name=\"FileName\" Value=\"(?<Location>.*?)\".*$",
29: RegexOptions.IgnoreCase |
30: RegexOptions.Multiline |
31: RegexOptions.Singleline |
32: RegexOptions.CultureInvariant |
33: RegexOptions.Compiled);
34: MatchCollection _Matches = _Regex.Matches(xmlToSearch);
35: return ((from Match m in _Matches
36: where m.Groups["Location"] != null
37: select m.Groups["Location"].Value).SingleOrDefault()) ?? "not found";
38: }
39:
40: private static string SearchLinqToXml(string xmlToSearch)
41: {
42: XElement _Root = XElement.Parse(xmlToSearch);
43: return _Root.Descendants("Element").
44: Where(n => n.Attribute("Name") != null &&
45: n.Attribute("Name").Value == "[MSDBLog]" &&
46: n.Element("Property").Attribute("Name").Value == "FileName").
47: Select(n => n.Element("Property").Attribute("Value").Value).SingleOrDefault() ?? "not found";
48: }