GridServerPostHandler.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using Nini.Config;
  28. using log4net;
  29. using System;
  30. using System.Reflection;
  31. using System.IO;
  32. using System.Net;
  33. using System.Text;
  34. using System.Text.RegularExpressions;
  35. using System.Xml;
  36. using System.Xml.Serialization;
  37. using System.Collections.Generic;
  38. using OpenSim.Server.Base;
  39. using OpenSim.Services.Interfaces;
  40. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  41. using OpenSim.Framework;
  42. using OpenSim.Framework.Servers.HttpServer;
  43. using OpenMetaverse;
  44. namespace OpenSim.Server.Handlers.Grid
  45. {
  46. public class GridServerPostHandler : BaseStreamHandler
  47. {
  48. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  49. private IGridService m_GridService;
  50. public GridServerPostHandler(IGridService service) :
  51. base("POST", "/grid")
  52. {
  53. m_GridService = service;
  54. }
  55. public override byte[] Handle(string path, Stream requestData,
  56. OSHttpRequest httpRequest, OSHttpResponse httpResponse)
  57. {
  58. StreamReader sr = new StreamReader(requestData);
  59. string body = sr.ReadToEnd();
  60. sr.Close();
  61. body = body.Trim();
  62. //m_log.DebugFormat("[XXX]: query String: {0}", body);
  63. try
  64. {
  65. Dictionary<string, object> request =
  66. ServerUtils.ParseQueryString(body);
  67. if (!request.ContainsKey("METHOD"))
  68. return FailureResult();
  69. string method = request["METHOD"].ToString();
  70. switch (method)
  71. {
  72. case "register":
  73. return Register(request);
  74. case "deregister":
  75. return Deregister(request);
  76. case "get_neighbours":
  77. return GetNeighbours(request);
  78. case "get_region_by_uuid":
  79. return GetRegionByUUID(request);
  80. case "get_region_by_position":
  81. return GetRegionByPosition(request);
  82. case "get_region_by_name":
  83. return GetRegionByName(request);
  84. case "get_regions_by_name":
  85. return GetRegionsByName(request);
  86. case "get_region_range":
  87. return GetRegionRange(request);
  88. case "get_default_regions":
  89. return GetDefaultRegions(request);
  90. case "get_fallback_regions":
  91. return GetFallbackRegions(request);
  92. case "get_region_flags":
  93. return GetRegionFlags(request);
  94. }
  95. m_log.DebugFormat("[GRID HANDLER]: unknown method {0} request {1}", method.Length, method);
  96. }
  97. catch (Exception e)
  98. {
  99. m_log.DebugFormat("[GRID HANDLER]: Exception {0}", e);
  100. }
  101. return FailureResult();
  102. }
  103. #region Method-specific handlers
  104. byte[] Register(Dictionary<string, object> request)
  105. {
  106. UUID scopeID = UUID.Zero;
  107. if (request.ContainsKey("SCOPEID"))
  108. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  109. else
  110. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to register region");
  111. int versionNumberMin = 0, versionNumberMax = 0;
  112. if (request.ContainsKey("VERSIONMIN"))
  113. Int32.TryParse(request["VERSIONMIN"].ToString(), out versionNumberMin);
  114. else
  115. m_log.WarnFormat("[GRID HANDLER]: no minimum protocol version in request to register region");
  116. if (request.ContainsKey("VERSIONMAX"))
  117. Int32.TryParse(request["VERSIONMAX"].ToString(), out versionNumberMax);
  118. else
  119. m_log.WarnFormat("[GRID HANDLER]: no maximum protocol version in request to register region");
  120. // Check the protocol version
  121. if ((versionNumberMin > ProtocolVersions.ServerProtocolVersionMax && versionNumberMax < ProtocolVersions.ServerProtocolVersionMax))
  122. {
  123. // Can't do, there is no overlap in the acceptable ranges
  124. return FailureResult();
  125. }
  126. Dictionary<string, object> rinfoData = new Dictionary<string, object>();
  127. GridRegion rinfo = null;
  128. try
  129. {
  130. foreach (KeyValuePair<string, object> kvp in request)
  131. rinfoData[kvp.Key] = kvp.Value.ToString();
  132. rinfo = new GridRegion(rinfoData);
  133. }
  134. catch (Exception e)
  135. {
  136. m_log.DebugFormat("[GRID HANDLER]: exception unpacking region data: {0}", e);
  137. }
  138. string result = "Error communicating with grid service";
  139. if (rinfo != null)
  140. result = m_GridService.RegisterRegion(scopeID, rinfo);
  141. if (result == String.Empty)
  142. return SuccessResult();
  143. else
  144. return FailureResult(result);
  145. }
  146. byte[] Deregister(Dictionary<string, object> request)
  147. {
  148. UUID regionID = UUID.Zero;
  149. if (request.ContainsKey("REGIONID"))
  150. UUID.TryParse(request["REGIONID"].ToString(), out regionID);
  151. else
  152. m_log.WarnFormat("[GRID HANDLER]: no regionID in request to deregister region");
  153. bool result = m_GridService.DeregisterRegion(regionID);
  154. if (result)
  155. return SuccessResult();
  156. else
  157. return FailureResult();
  158. }
  159. byte[] GetNeighbours(Dictionary<string, object> request)
  160. {
  161. UUID scopeID = UUID.Zero;
  162. if (request.ContainsKey("SCOPEID"))
  163. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  164. else
  165. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get neighbours");
  166. UUID regionID = UUID.Zero;
  167. if (request.ContainsKey("REGIONID"))
  168. UUID.TryParse(request["REGIONID"].ToString(), out regionID);
  169. else
  170. m_log.WarnFormat("[GRID HANDLER]: no regionID in request to get neighbours");
  171. List<GridRegion> rinfos = m_GridService.GetNeighbours(scopeID, regionID);
  172. //m_log.DebugFormat("[GRID HANDLER]: neighbours for region {0}: {1}", regionID, rinfos.Count);
  173. Dictionary<string, object> result = new Dictionary<string, object>();
  174. if ((rinfos == null) || ((rinfos != null) && (rinfos.Count == 0)))
  175. result["result"] = "null";
  176. else
  177. {
  178. int i = 0;
  179. foreach (GridRegion rinfo in rinfos)
  180. {
  181. Dictionary<string, object> rinfoDict = rinfo.ToKeyValuePairs();
  182. result["region" + i] = rinfoDict;
  183. i++;
  184. }
  185. }
  186. string xmlString = ServerUtils.BuildXmlResponse(result);
  187. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  188. UTF8Encoding encoding = new UTF8Encoding();
  189. return encoding.GetBytes(xmlString);
  190. }
  191. byte[] GetRegionByUUID(Dictionary<string, object> request)
  192. {
  193. UUID scopeID = UUID.Zero;
  194. if (request.ContainsKey("SCOPEID"))
  195. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  196. else
  197. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get neighbours");
  198. UUID regionID = UUID.Zero;
  199. if (request.ContainsKey("REGIONID"))
  200. UUID.TryParse(request["REGIONID"].ToString(), out regionID);
  201. else
  202. m_log.WarnFormat("[GRID HANDLER]: no regionID in request to get neighbours");
  203. GridRegion rinfo = m_GridService.GetRegionByUUID(scopeID, regionID);
  204. //m_log.DebugFormat("[GRID HANDLER]: neighbours for region {0}: {1}", regionID, rinfos.Count);
  205. Dictionary<string, object> result = new Dictionary<string, object>();
  206. if (rinfo == null)
  207. result["result"] = "null";
  208. else
  209. result["result"] = rinfo.ToKeyValuePairs();
  210. string xmlString = ServerUtils.BuildXmlResponse(result);
  211. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  212. UTF8Encoding encoding = new UTF8Encoding();
  213. return encoding.GetBytes(xmlString);
  214. }
  215. byte[] GetRegionByPosition(Dictionary<string, object> request)
  216. {
  217. UUID scopeID = UUID.Zero;
  218. if (request.ContainsKey("SCOPEID"))
  219. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  220. else
  221. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get region by position");
  222. int x = 0, y = 0;
  223. if (request.ContainsKey("X"))
  224. Int32.TryParse(request["X"].ToString(), out x);
  225. else
  226. m_log.WarnFormat("[GRID HANDLER]: no X in request to get region by position");
  227. if (request.ContainsKey("Y"))
  228. Int32.TryParse(request["Y"].ToString(), out y);
  229. else
  230. m_log.WarnFormat("[GRID HANDLER]: no Y in request to get region by position");
  231. GridRegion rinfo = m_GridService.GetRegionByPosition(scopeID, x, y);
  232. //m_log.DebugFormat("[GRID HANDLER]: neighbours for region {0}: {1}", regionID, rinfos.Count);
  233. Dictionary<string, object> result = new Dictionary<string, object>();
  234. if (rinfo == null)
  235. result["result"] = "null";
  236. else
  237. result["result"] = rinfo.ToKeyValuePairs();
  238. string xmlString = ServerUtils.BuildXmlResponse(result);
  239. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  240. UTF8Encoding encoding = new UTF8Encoding();
  241. return encoding.GetBytes(xmlString);
  242. }
  243. byte[] GetRegionByName(Dictionary<string, object> request)
  244. {
  245. UUID scopeID = UUID.Zero;
  246. if (request.ContainsKey("SCOPEID"))
  247. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  248. else
  249. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get region by name");
  250. string regionName = string.Empty;
  251. if (request.ContainsKey("NAME"))
  252. regionName = request["NAME"].ToString();
  253. else
  254. m_log.WarnFormat("[GRID HANDLER]: no name in request to get region by name");
  255. GridRegion rinfo = m_GridService.GetRegionByName(scopeID, regionName);
  256. //m_log.DebugFormat("[GRID HANDLER]: neighbours for region {0}: {1}", regionID, rinfos.Count);
  257. Dictionary<string, object> result = new Dictionary<string, object>();
  258. if (rinfo == null)
  259. result["result"] = "null";
  260. else
  261. result["result"] = rinfo.ToKeyValuePairs();
  262. string xmlString = ServerUtils.BuildXmlResponse(result);
  263. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  264. UTF8Encoding encoding = new UTF8Encoding();
  265. return encoding.GetBytes(xmlString);
  266. }
  267. byte[] GetRegionsByName(Dictionary<string, object> request)
  268. {
  269. UUID scopeID = UUID.Zero;
  270. if (request.ContainsKey("SCOPEID"))
  271. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  272. else
  273. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get regions by name");
  274. string regionName = string.Empty;
  275. if (request.ContainsKey("NAME"))
  276. regionName = request["NAME"].ToString();
  277. else
  278. m_log.WarnFormat("[GRID HANDLER]: no NAME in request to get regions by name");
  279. int max = 0;
  280. if (request.ContainsKey("MAX"))
  281. Int32.TryParse(request["MAX"].ToString(), out max);
  282. else
  283. m_log.WarnFormat("[GRID HANDLER]: no MAX in request to get regions by name");
  284. List<GridRegion> rinfos = m_GridService.GetRegionsByName(scopeID, regionName, max);
  285. //m_log.DebugFormat("[GRID HANDLER]: neighbours for region {0}: {1}", regionID, rinfos.Count);
  286. Dictionary<string, object> result = new Dictionary<string, object>();
  287. if ((rinfos == null) || ((rinfos != null) && (rinfos.Count == 0)))
  288. result["result"] = "null";
  289. else
  290. {
  291. int i = 0;
  292. foreach (GridRegion rinfo in rinfos)
  293. {
  294. Dictionary<string, object> rinfoDict = rinfo.ToKeyValuePairs();
  295. result["region" + i] = rinfoDict;
  296. i++;
  297. }
  298. }
  299. string xmlString = ServerUtils.BuildXmlResponse(result);
  300. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  301. UTF8Encoding encoding = new UTF8Encoding();
  302. return encoding.GetBytes(xmlString);
  303. }
  304. byte[] GetRegionRange(Dictionary<string, object> request)
  305. {
  306. //m_log.DebugFormat("[GRID HANDLER]: GetRegionRange");
  307. UUID scopeID = UUID.Zero;
  308. if (request.ContainsKey("SCOPEID"))
  309. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  310. else
  311. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get region range");
  312. int xmin = 0, xmax = 0, ymin = 0, ymax = 0;
  313. if (request.ContainsKey("XMIN"))
  314. Int32.TryParse(request["XMIN"].ToString(), out xmin);
  315. else
  316. m_log.WarnFormat("[GRID HANDLER]: no XMIN in request to get region range");
  317. if (request.ContainsKey("XMAX"))
  318. Int32.TryParse(request["XMAX"].ToString(), out xmax);
  319. else
  320. m_log.WarnFormat("[GRID HANDLER]: no XMAX in request to get region range");
  321. if (request.ContainsKey("YMIN"))
  322. Int32.TryParse(request["YMIN"].ToString(), out ymin);
  323. else
  324. m_log.WarnFormat("[GRID HANDLER]: no YMIN in request to get region range");
  325. if (request.ContainsKey("YMAX"))
  326. Int32.TryParse(request["YMAX"].ToString(), out ymax);
  327. else
  328. m_log.WarnFormat("[GRID HANDLER]: no YMAX in request to get region range");
  329. List<GridRegion> rinfos = m_GridService.GetRegionRange(scopeID, xmin, xmax, ymin, ymax);
  330. Dictionary<string, object> result = new Dictionary<string, object>();
  331. if ((rinfos == null) || ((rinfos != null) && (rinfos.Count == 0)))
  332. result["result"] = "null";
  333. else
  334. {
  335. int i = 0;
  336. foreach (GridRegion rinfo in rinfos)
  337. {
  338. Dictionary<string, object> rinfoDict = rinfo.ToKeyValuePairs();
  339. result["region" + i] = rinfoDict;
  340. i++;
  341. }
  342. }
  343. string xmlString = ServerUtils.BuildXmlResponse(result);
  344. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  345. UTF8Encoding encoding = new UTF8Encoding();
  346. return encoding.GetBytes(xmlString);
  347. }
  348. byte[] GetDefaultRegions(Dictionary<string, object> request)
  349. {
  350. //m_log.DebugFormat("[GRID HANDLER]: GetDefaultRegions");
  351. UUID scopeID = UUID.Zero;
  352. if (request.ContainsKey("SCOPEID"))
  353. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  354. else
  355. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get region range");
  356. List<GridRegion> rinfos = m_GridService.GetDefaultRegions(scopeID);
  357. Dictionary<string, object> result = new Dictionary<string, object>();
  358. if ((rinfos == null) || ((rinfos != null) && (rinfos.Count == 0)))
  359. result["result"] = "null";
  360. else
  361. {
  362. int i = 0;
  363. foreach (GridRegion rinfo in rinfos)
  364. {
  365. Dictionary<string, object> rinfoDict = rinfo.ToKeyValuePairs();
  366. result["region" + i] = rinfoDict;
  367. i++;
  368. }
  369. }
  370. string xmlString = ServerUtils.BuildXmlResponse(result);
  371. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  372. UTF8Encoding encoding = new UTF8Encoding();
  373. return encoding.GetBytes(xmlString);
  374. }
  375. byte[] GetFallbackRegions(Dictionary<string, object> request)
  376. {
  377. //m_log.DebugFormat("[GRID HANDLER]: GetRegionRange");
  378. UUID scopeID = UUID.Zero;
  379. if (request.ContainsKey("SCOPEID"))
  380. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  381. else
  382. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get fallback regions");
  383. int x = 0, y = 0;
  384. if (request.ContainsKey("X"))
  385. Int32.TryParse(request["X"].ToString(), out x);
  386. else
  387. m_log.WarnFormat("[GRID HANDLER]: no X in request to get fallback regions");
  388. if (request.ContainsKey("Y"))
  389. Int32.TryParse(request["Y"].ToString(), out y);
  390. else
  391. m_log.WarnFormat("[GRID HANDLER]: no Y in request to get fallback regions");
  392. List<GridRegion> rinfos = m_GridService.GetFallbackRegions(scopeID, x, y);
  393. Dictionary<string, object> result = new Dictionary<string, object>();
  394. if ((rinfos == null) || ((rinfos != null) && (rinfos.Count == 0)))
  395. result["result"] = "null";
  396. else
  397. {
  398. int i = 0;
  399. foreach (GridRegion rinfo in rinfos)
  400. {
  401. Dictionary<string, object> rinfoDict = rinfo.ToKeyValuePairs();
  402. result["region" + i] = rinfoDict;
  403. i++;
  404. }
  405. }
  406. string xmlString = ServerUtils.BuildXmlResponse(result);
  407. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  408. UTF8Encoding encoding = new UTF8Encoding();
  409. return encoding.GetBytes(xmlString);
  410. }
  411. byte[] GetRegionFlags(Dictionary<string, object> request)
  412. {
  413. UUID scopeID = UUID.Zero;
  414. if (request.ContainsKey("SCOPEID"))
  415. UUID.TryParse(request["SCOPEID"].ToString(), out scopeID);
  416. else
  417. m_log.WarnFormat("[GRID HANDLER]: no scopeID in request to get neighbours");
  418. UUID regionID = UUID.Zero;
  419. if (request.ContainsKey("REGIONID"))
  420. UUID.TryParse(request["REGIONID"].ToString(), out regionID);
  421. else
  422. m_log.WarnFormat("[GRID HANDLER]: no regionID in request to get neighbours");
  423. int flags = m_GridService.GetRegionFlags(scopeID, regionID);
  424. // m_log.DebugFormat("[GRID HANDLER]: flags for region {0}: {1}", regionID, flags);
  425. Dictionary<string, object> result = new Dictionary<string, object>();
  426. result["result"] = flags.ToString();
  427. string xmlString = ServerUtils.BuildXmlResponse(result);
  428. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  429. UTF8Encoding encoding = new UTF8Encoding();
  430. return encoding.GetBytes(xmlString);
  431. }
  432. #endregion
  433. #region Misc
  434. private byte[] SuccessResult()
  435. {
  436. XmlDocument doc = new XmlDocument();
  437. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  438. "", "");
  439. doc.AppendChild(xmlnode);
  440. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  441. "");
  442. doc.AppendChild(rootElement);
  443. XmlElement result = doc.CreateElement("", "Result", "");
  444. result.AppendChild(doc.CreateTextNode("Success"));
  445. rootElement.AppendChild(result);
  446. return DocToBytes(doc);
  447. }
  448. private byte[] FailureResult()
  449. {
  450. return FailureResult(String.Empty);
  451. }
  452. private byte[] FailureResult(string msg)
  453. {
  454. XmlDocument doc = new XmlDocument();
  455. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  456. "", "");
  457. doc.AppendChild(xmlnode);
  458. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  459. "");
  460. doc.AppendChild(rootElement);
  461. XmlElement result = doc.CreateElement("", "Result", "");
  462. result.AppendChild(doc.CreateTextNode("Failure"));
  463. rootElement.AppendChild(result);
  464. XmlElement message = doc.CreateElement("", "Message", "");
  465. message.AppendChild(doc.CreateTextNode(msg));
  466. rootElement.AppendChild(message);
  467. return DocToBytes(doc);
  468. }
  469. private byte[] DocToBytes(XmlDocument doc)
  470. {
  471. MemoryStream ms = new MemoryStream();
  472. XmlTextWriter xw = new XmlTextWriter(ms, null);
  473. xw.Formatting = Formatting.Indented;
  474. doc.WriteTo(xw);
  475. xw.Flush();
  476. return ms.ToArray();
  477. }
  478. #endregion
  479. }
  480. }