XmlTODatSet.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Xml;
  7. using System.Data;
  8. namespace ETD.Frame
  9. {
  10. public class XmlTODatSet
  11. {
  12. /// <summary>
  13. /// xml转DataTable
  14. /// </summary>
  15. /// <param name="xmlDS"></param>
  16. /// <returns></returns>
  17. private string ConvertDataTableToXML(DataTable xmlDS)
  18. {
  19. MemoryStream stream = null;
  20. XmlTextWriter writer = null;
  21. try
  22. {
  23. stream = new MemoryStream();
  24. writer = new XmlTextWriter(stream, Encoding.Default);
  25. xmlDS.WriteXml(writer);
  26. int count = (int)stream.Length;
  27. byte[] arr = new byte[count];
  28. stream.Seek(0, SeekOrigin.Begin);
  29. stream.Read(arr, 0, count);
  30. UTF8Encoding utf = new UTF8Encoding();
  31. return utf.GetString(arr).Trim();
  32. }
  33. catch
  34. {
  35. return String.Empty;
  36. }
  37. finally
  38. {
  39. if (writer != null) writer.Close();
  40. }
  41. }
  42. /// <summary>
  43. /// XML转DataSet
  44. /// </summary>
  45. /// <param name="xmlData"></param>
  46. /// <returns></returns>
  47. private DataSet ConvertXMLToDataSet(string xmlData)
  48. {
  49. StringReader stream = null;
  50. XmlTextReader reader = null;
  51. try
  52. {
  53. DataSet xmlDS = new DataSet();
  54. stream = new StringReader(xmlData);
  55. reader = new XmlTextReader(stream);
  56. xmlDS.ReadXml(reader);
  57. return xmlDS;
  58. }
  59. catch (Exception ex)
  60. {
  61. string strTest = ex.Message;
  62. return null;
  63. }
  64. finally
  65. {
  66. if (reader != null)
  67. reader.Close();
  68. }
  69. }
  70. }
  71. }