XmlTreeView.cs
7.33 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//*************************************************************************************************
// XmlTreeView.cs
// Owner: Mahipal Kante
//
// Wrapper for TreeView that bings XML to TreeView
//
// Copyright(c) Microsoft Corporation, 2010
//*************************************************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
namespace WebTest.WebService.Plugin.ResultTab
{
/// <summary>
/// Wrapper for TreeView to enable XML content view
/// </summary>
public class XmlTreeView
{
private TreeView treeView;
private XmlDocument dom;
/// <summary>
/// Wrapper for TreeView to enable XML content view
/// </summary>
/// <param name="treeView">TreeView where XML content needs to be shown</param>
public XmlTreeView(TreeView treeView)
{
this.treeView = treeView;
treeView.BeforeExpand += new TreeViewCancelEventHandler(TreeExpanded);
treeView.AfterLabelEdit += new NodeLabelEditEventHandler(treeViewRequest_AfterLabelEdit);
}
/// <summary>
/// Show XML in tree
/// </summary>
/// <param name="xmlString"></param>
/// <returns></returns>
public XmlDocument ShowXML(string xmlString)
{
try
{
// Load the XML file.
this.dom = new XmlDocument();
dom.LoadXml(xmlString);
ShowXML(dom);
return dom;
}
catch (Exception)
{
//MessageBox.Show(ex.Message, "Error");
}
return null;
}
/// <summary>
/// Show XML in tree
/// </summary>
/// <param name="reader"></param>
/// <returns></returns>
public XmlDocument ShowXML(XmlReader reader)
{
try
{
// Load the XML file.
this.dom = new XmlDocument();
dom.Load(reader);
ShowXML(dom);
return dom;
}
catch (Exception)
{
//MessageBox.Show(ex.Message, "Error");
}
return null;
}
/// <summary>
/// Show XML in tree
/// </summary>
/// <param name="dom"></param>
public void ShowXML(XmlDocument dom)
{
try
{
this.dom = dom;
if (dom.DocumentElement != null)
{
XmlNode xNode = dom.DocumentElement;
// Load the XML into the TreeView.
treeView.Nodes.Clear();
TreeNode treeNode = new TreeNode(xNode.Name);
treeNode.Tag = xNode;
if (xNode.HasChildNodes)
{
treeNode.Nodes.Add(new TreeNode("dummy"));
}
else
{
treeNode.Text = xNode.InnerXml.Trim();
}
treeView.Nodes.Add(treeNode);
treeNode.Collapse();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}
}
/// <summary>
/// Indicates whether message has been edited since last update
/// </summary>
public bool MessageEdited
{
get;
set;
}
/// <summary>
/// replaces currently selected XML node's contents with given parameter name
/// </summary>
/// <param name="parameterName">name of the parameter</param>
public void AddParameter(string parameterName)
{
TreeNode treeNode = this.treeView.SelectedNode;
TreeNode parentTreeNode = treeNode.Parent;
if (treeNode != null && parentTreeNode != null)
{
if (parameterName.Equals("Type Parameter"))
{
if (!treeNode.IsEditing)
{
treeNode.BeginEdit();
}
return;
}
ParameterizeNode(treeNode, "{{" + parameterName + "}}");
}
}
private void treeViewRequest_AfterLabelEdit(object sender, System.Windows.Forms.NodeLabelEditEventArgs e)
{
if (e.Label != null)
{
if (e.Label.Length > 0)
{
//Stop editing without canceling the label change.
e.Node.EndEdit(false);
ParameterizeNode(e.Node, e.Label);
}
else
{
// Cancel the label edit action, inform the user, and
// place the node in edit mode again
e.CancelEdit = true;
MessageBox.Show("Invalid input.\nThe label cannot be blank",
"Node Label Edit");
e.Node.BeginEdit();
}
}
}
private void TreeExpanded(object sender, TreeViewCancelEventArgs e)
{
if (e.Node.Nodes.Count == 1 && e.Node.Nodes[0].Text.Equals("dummy"))
{
e.Node.Nodes.Clear();
XmlNode parentNode = e.Node.Tag as XmlNode;
XmlNodeList nodeList = parentNode.ChildNodes;
for (int i = 0; i <= nodeList.Count - 1; i++)
{
XmlNode xNode = parentNode.ChildNodes[i];
TreeNode treeNode = new TreeNode();
treeNode.Tag = xNode;
if (xNode.NodeType.Equals(XmlNodeType.Element))
{
treeNode.Text = xNode.Name;
if (xNode.HasChildNodes)
{
treeNode.Nodes.Add(new TreeNode("dummy"));
}
else
{
treeNode.Nodes.Add(new TreeNode(xNode.Value));
}
}
else
{
treeNode.Text = xNode.Value;
}
e.Node.Nodes.Add(treeNode);
treeNode.Collapse();
}
}
}
private void ParameterizeNode(TreeNode treeNode, string parameter)
{
TreeNode parentTreeNode = treeNode.Parent;
XmlNode xNode = treeNode.Tag as XmlNode;
if (xNode != null)
{
XmlNode parentXNode = parentTreeNode.Tag as XmlNode;
treeNode.Text = parameter;
if (xNode is XmlText)
{
xNode.Value = parameter;
}
else
{
XmlText textNode = dom.CreateTextNode(parameter);
parentXNode.ReplaceChild(textNode, xNode);
treeNode.Tag = textNode;
treeNode.Nodes.Clear();
}
this.MessageEdited = true;
}
}
}
}