ContentHandler.cs
6.25 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
//*************************************************************************************************
// ContentHandler.cs
// Owner: Mahipal Kante
//
// Base class for handling a content type
//
// Copyright(c) Microsoft Corporation, 2010
//*************************************************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Xml.XPath;
namespace WebTest.WebService.Plugin.Runtime
{
/// <summary>
/// Base class for handling a content type
/// </summary>
public class ContentHandler
{
private string xmlString = string.Empty;
private XmlDocument xmlDocument = null;
/// <summary>
/// Is it a binary format?
/// </summary>
public virtual bool IsBinary
{
get
{
return false;
}
}
/// <summary>
/// Raw binary content
/// </summary>
public virtual Byte[] MessageBytes
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
/// <summary>
/// Raw string content
/// </summary>
public virtual string MessageString
{
get
{
return XmlToString(this.XmlDocument);
}
set
{
this.xmlString = value;
this.xmlDocument = null;
}
}
/// <summary>
/// message converted to XML
/// </summary>
public string XmlString
{
get
{
return XmlToIndentedString(this.XmlDocument);
}
set
{
this.xmlString = value;
this.xmlDocument = null;
}
}
/// <summary>
/// XML Document of the message
/// </summary>
public XmlDocument XmlDocument
{
get
{
if (this.xmlDocument == null)
{
if (string.IsNullOrEmpty(this.xmlString))
{
this.LoadDocument();
}
else
{
try
{
this.xmlDocument = new XmlDocument();
this.xmlDocument.LoadXml(this.xmlString);
}
catch (Exception)
{
// Try setting it as a native format message
this.MessageString = this.xmlString;
this.LoadDocument();
}
}
}
return xmlDocument;
}
set
{
this.xmlDocument = value;
}
}
/// <summary>
/// XPath document of XML
/// </summary>
public virtual XPathDocument XPathDocument
{
get
{
return new XPathDocument(new StringReader(this.xmlString));
}
}
/// <summary>
/// Evaluates XPath
/// </summary>
/// <param name="xPath">xPath expression</param>
/// <returns></returns>
public XPathNodeIterator EvaluateXPath(string xPath)
{
XPathDocument x = this.XPathDocument;
XPathNavigator nav = x.CreateNavigator();
XmlNamespaceManager context = new XmlNamespaceManager(nav.NameTable);
while (nav.MoveToFollowing(XPathNodeType.Element))
{
IDictionary<string, string> whatever = nav.GetNamespacesInScope(XmlNamespaceScope.All);
foreach (KeyValuePair<string, string> key in whatever)
{
context.AddNamespace(key.Key, key.Value);
}
}
// Compile a standard XPath expression
XPathExpression expr;
expr = nav.Compile(xPath);
expr.SetContext(context);
return nav.Select(expr);
}
/// <summary>
/// Override to perform custom load
/// </summary>
protected virtual void LoadDocument()
{
}
/// <summary>
/// Converts XmlDocument to String
/// </summary>
/// <param name="dom"></param>
/// <returns></returns>
public static string XmlToString(XmlDocument dom)
{
string message = string.Empty;
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
using (var stream = new MemoryStream())
{
using (XmlTextWriter writer = new XmlTextWriter(stream, encoding))
{
dom.WriteTo(writer);
}
message = encoding.GetString(stream.ToArray());
}
return message;
}
/// <summary>
/// Converts XmlDocument to String
/// </summary>
/// <param name="dom"></param>
/// <returns></returns>
public static string XmlToIndentedString(XmlDocument dom)
{
string message = string.Empty;
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
using (var stream = new MemoryStream())
{
using (XmlTextWriter writer = new XmlTextWriter(stream, encoding))
{
writer.Formatting = Formatting.Indented;
dom.WriteTo(writer);
}
message = encoding.GetString(stream.ToArray());
}
return message;
}
/// <summary>
/// Parses XML in string form and returns XmlDocument
/// </summary>
/// <param name="xmlString"></param>
/// <returns></returns>
public static XmlDocument StringToXml(string xmlString)
{
XmlDocument dom = new XmlDocument();
dom.LoadXml(xmlString);
return dom;
}
}
}