-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainWindow.xaml.cs
117 lines (102 loc) · 3.88 KB
/
MainWindow.xaml.cs
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
namespace XPathQuery
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
txtXMLFile_TextChanged(null, null);
txtXPath_TextChanged(null, null);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
try
{
List<Namespace> namespaces = new List<Namespace>();
foreach (var line in txtNamespaces.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
namespaces.Add(Namespace.FromString(line));
txtNamespaceCount.Text = string.Format("{0} namespace{1}", namespaces.Count, namespaces.Count == 1 ? string.Empty : "s");
XPathProcessorResult result = XPathProcessor.Process(txtXMLFile.Text, txtXPath.Text, namespaces);
txtResult.Text = result.Result;
txtResultCount.Text = string.Format("{0} result{1}", result.Count, result.Count == 1 ? string.Empty : "s");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error processing XPath", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void txtXMLFile_TextChanged(object sender, TextChangedEventArgs e)
{
if (!File.Exists(txtXMLFile.Text))
{
txtXMLFile.Style = (Style)this.FindResource("ErrorTextBoxStyle");
return;
}
XDocument doc;
try
{
doc = XDocument.Load(txtXMLFile.Text);
}
catch
{
txtXMLFile.Style = (Style)this.FindResource("ErrorTextBoxStyle");
return;
}
XPathNavigator nav = doc.CreateNavigator();
nav.MoveToFollowing(XPathNodeType.Element);
IDictionary<string, string> ns = nav.GetNamespacesInScope(XmlNamespaceScope.All);
if (ns.Any())
{
StringBuilder sb = new StringBuilder();
foreach (var pair in ns)
{
if (pair.Key == string.Empty)
sb.AppendLine(string.Format("default=\"{0}\"", pair.Value));
else
sb.AppendLine(string.Format("{0}=\"{1}\"", pair.Key, pair.Value));
}
txtNamespaces.Text = sb.ToString().TrimEnd();
}
txtXMLFile.Style = (Style)this.FindResource("TextBoxStyle");
}
private void txtXMLFile_PreviewDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] droppedFilePaths = e.Data.GetData(DataFormats.FileDrop, true) as string[];
txtXMLFile.Text = droppedFilePaths[0]; // Only use first file
}
}
private void txtXMLFile_PreviewDrag(object sender, DragEventArgs e)
{
e.Effects = DragDropEffects.All;
e.Handled = true;
}
private void txtXPath_TextChanged(object sender, TextChangedEventArgs e)
{
XmlDocument doc = new XmlDocument();
XPathNavigator nav = doc.CreateNavigator();
try
{
XPathExpression expr = nav.Compile(txtXPath.Text);
}
catch (XPathException)
{
txtXPath.Style = (Style)this.FindResource("ErrorTextBoxStyle");
return;
}
txtXPath.Style = (Style)this.FindResource("TextBoxStyle");
}
}
}