3using System.Collections.Generic;
7using System.Xml.Schema;
10using System.Windows.Controls;
11using System.Windows.Data;
28 private Field[] headers;
29 private Dictionary<string, string>[] records;
30 private DataTable table;
41 this.Init(Headers, Records);
44 private void Init(
Field[] Headers, Dictionary<string, string>[] Records)
46 this.headers = Headers;
47 this.records = Records;
51 if (!(this.table is
null))
57 this.table =
new DataTable(
"SearchResult");
59 foreach (
Field Header
in Headers)
60 this.table.Columns.Add(Header.
Var);
62 foreach (Dictionary<string, string> Record
in Records)
64 DataRow Row = this.table.NewRow();
66 foreach (KeyValuePair<string, string> P
in Record)
69 this.table.Rows.Add(Row);
72 this.table.AcceptChanges();
74 this.SearchResultListView.ItemsSource = this.table.DefaultView;
76 if (this.SearchResultListView.View is GridView GridView)
78 GridView.Columns.Clear();
80 foreach (
Field Header
in this.headers)
82 GridView.Columns.Add(
new GridViewColumn()
84 Header = Header.
Label,
85 DisplayMemberBinding =
new Binding(Header.
Var)
93 if (!(this.table is
null))
100 public void NewButton_Click(
object Sender, RoutedEventArgs e)
105 public void SaveButton_Click(
object Sender, RoutedEventArgs e)
107 this.SaveAsButton_Click(Sender, e);
110 public void SaveAsButton_Click(
object Sender, RoutedEventArgs e)
112 SaveFileDialog Dialog =
new SaveFileDialog()
115 CheckPathExists =
true,
116 CreatePrompt =
false,
118 Filter =
"XML Files (*.xml)|*.xml|HTML Files (*.html,*.htm)|*.html,*.htm|All Files (*.*)|*.*",
119 Title =
"Save Search Result"
122 bool? Result = Dialog.ShowDialog(
MainWindow.FindWindow(
this));
124 if (Result.HasValue && Result.Value)
128 if (Dialog.FilterIndex == 2)
130 StringBuilder Xml =
new StringBuilder();
136 string Html =
XSL.
Transform(Xml.ToString(), searchResultToHtml);
138 File.WriteAllText(Dialog.FileName, Html, System.Text.Encoding.UTF8);
142 using (FileStream f = File.Create(Dialog.FileName))
153 MessageBox.Show(
MainWindow.FindWindow(
this), ex.Message,
"Unable to save file.", MessageBoxButton.OK, MessageBoxImage.Error);
158 private static readonly XslCompiledTransform searchResultToHtml =
XSL.
LoadTransform(
"Waher.Client.WPF.Transforms.SearchResultToHTML.xslt");
159 private static readonly XmlSchema schema =
XSL.
LoadSchema(
"Waher.Client.WPF.Schema.SearchResult.xsd");
160 private const string searchResultNamespace =
"http://waher.se/Schema/SearchResult.xsd";
161 private const string searchResultRoot =
"SearchResult";
163 private void SaveAsXml(XmlWriter w)
165 w.WriteStartElement(searchResultRoot, searchResultNamespace);
166 w.WriteStartElement(
"Headers");
168 foreach (
Field Header
in this.headers)
170 w.WriteStartElement(
"Header");
171 w.WriteAttributeString(
"var", Header.
Var);
172 w.WriteAttributeString(
"label", Header.
Label);
177 w.WriteStartElement(
"Records");
179 foreach (Dictionary<string, string> Record
in this.records)
181 w.WriteStartElement(
"Record");
183 foreach (KeyValuePair<string, string>
Field in Record)
185 w.WriteStartElement(
"Field");
186 w.WriteAttributeString(
"var",
Field.Key);
187 w.WriteAttributeString(
"value",
Field.Value);
199 public void OpenButton_Click(
object Sender, RoutedEventArgs e)
203 OpenFileDialog Dialog =
new OpenFileDialog()
206 CheckFileExists =
true,
207 CheckPathExists =
true,
209 Filter =
"XML Files (*.xml)|*.xml|All Files (*.*)|*.*",
212 Title =
"Open Search Result"
215 bool? Result = Dialog.ShowDialog(
MainWindow.FindWindow(
this));
217 if (Result.HasValue && Result.Value)
219 XmlDocument Xml =
new XmlDocument()
221 PreserveWhitespace =
true
223 Xml.Load(Dialog.FileName);
225 this.Load(Xml, Dialog.FileName);
231 MessageBox.Show(ex.Message,
"Unable to load file.", MessageBoxButton.OK, MessageBoxImage.Error);
235 public void Load(XmlDocument Xml,
string FileName)
237 XSL.
Validate(FileName, Xml, searchResultRoot, searchResultNamespace, schema);
239 List<Field> Headers =
new List<Field>();
240 List<Dictionary<string, string>> Records =
new List<Dictionary<string, string>>();
242 foreach (XmlNode N
in Xml.DocumentElement.ChildNodes)
244 if (N is XmlElement E)
249 foreach (XmlNode N2
in E.ChildNodes)
251 if (N2 is XmlElement E2 && E2.LocalName ==
"Header")
256 Headers.Add(
new TextSingleField(
null, Var, Label,
false,
null,
null,
string.Empty,
263 foreach (XmlNode N2
in E.ChildNodes)
265 if (N2 is XmlElement E2 && E2.LocalName ==
"Record")
267 Dictionary<string, string> Record =
new Dictionary<string, string>();
269 foreach (XmlNode N3
in E2.ChildNodes)
271 if (N3 is XmlElement E3 && E3.LocalName ==
"Field")
288 this.Init(Headers.ToArray(), Records.ToArray());
Interaction logic for SensorDataView.xaml
void InitializeComponent()
InitializeComponent
Interaction logic for xaml
Helps with common XML-related tasks.
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
static XmlWriterSettings WriterSettings(bool Indent, bool OmitXmlDeclaration)
Gets an XML writer settings object.
Static class managing loading of XSL resources stored as embedded resources or in content files.
static XmlSchema LoadSchema(string ResourceName)
Loads an XML schema from an embedded resource.
static XslCompiledTransform LoadTransform(string ResourceName)
Loads an XSL transformation from an embedded resource.
static string Transform(string XML, XslCompiledTransform Transform)
Transforms an XML document using an XSL transform.
static void Validate(string ObjectID, XmlDocument Xml, params XmlSchema[] Schemas)
Validates an XML document given a set of XML schemas.
Static class managing the application event log. Applications and services log events on this static ...
static Exception UnnestException(Exception Exception)
Unnests an exception, to extract the relevant inner exception.
Interface for tab view user controls in the client.