Json2Xml.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using System.Xml;
  5. using System.Xml.Serialization;
  6. namespace TEAMModelOS.SDK.Helper.Common.XmlHelper
  7. {
  8. public class Json2Xml
  9. {
  10. [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.7.2612.0")]
  11. [System.SerializableAttribute()]
  12. [System.Xml.Serialization.XmlTypeAttribute(Namespace = "urn:ebay:apis:eBLBaseComponents")]
  13. public enum ItemSpecificSourceCodeType
  14. {
  15. /// <remarks/>
  16. ItemSpecific,
  17. /// <remarks/>
  18. Attribute,
  19. /// <remarks/>
  20. Product,
  21. /// <remarks/>
  22. CustomCode,
  23. }
  24. [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.7.2612.0")]
  25. [System.SerializableAttribute()]
  26. [System.Diagnostics.DebuggerStepThroughAttribute()]
  27. [System.ComponentModel.DesignerCategoryAttribute("code")]
  28. [System.Xml.Serialization.XmlTypeAttribute(Namespace = "urn:ebay:apis:eBLBaseComponents")]
  29. public partial class NameValueListType
  30. {
  31. private string nameField;
  32. private string[] valueField;
  33. private ItemSpecificSourceCodeType sourceField;
  34. private bool sourceFieldSpecified;
  35. private System.Xml.XmlElement[] anyField;
  36. /// <remarks/>
  37. public string Name
  38. {
  39. get
  40. {
  41. return this.nameField;
  42. }
  43. set
  44. {
  45. this.nameField = value;
  46. }
  47. }
  48. /// <remarks/>
  49. [System.Xml.Serialization.XmlElementAttribute("Value")]
  50. public string[] Value
  51. {
  52. get
  53. {
  54. return this.valueField;
  55. }
  56. set
  57. {
  58. this.valueField = value;
  59. }
  60. }
  61. /// <remarks/>
  62. public ItemSpecificSourceCodeType Source
  63. {
  64. get
  65. {
  66. return this.sourceField;
  67. }
  68. set
  69. {
  70. this.sourceField = value;
  71. }
  72. }
  73. /// <remarks/>
  74. [System.Xml.Serialization.XmlIgnoreAttribute()]
  75. public bool SourceSpecified
  76. {
  77. get
  78. {
  79. return this.sourceFieldSpecified;
  80. }
  81. set
  82. {
  83. this.sourceFieldSpecified = value;
  84. }
  85. }
  86. /// <remarks/>
  87. [System.Xml.Serialization.XmlAnyElementAttribute()]
  88. public System.Xml.XmlElement[] Any
  89. {
  90. get
  91. {
  92. return this.anyField;
  93. }
  94. set
  95. {
  96. this.anyField = value;
  97. }
  98. }
  99. }
  100. public class CustomItemSpecifics
  101. {
  102. public NameValueListType[] ItemSpecifics;
  103. }
  104. public class Item
  105. {
  106. public string Name { get; set; }
  107. public string Value { get; set; }
  108. public string[] RecommValues { get; set; }
  109. //public void DataTable2Entity(Item item, DataRow dr) {
  110. // item.Name=dr.
  111. //}
  112. }
  113. public static string ObjectToText(Object inObject, Type inType)
  114. {
  115. String XmlizedString = null;
  116. MemoryStream ms = new MemoryStream();
  117. XmlSerializer xs = new XmlSerializer(inType);
  118. XmlTextWriter xmlTextWriter = new XmlTextWriter(ms, Encoding.UTF8);
  119. xs.Serialize(xmlTextWriter, inObject);
  120. ms = (MemoryStream)xmlTextWriter.BaseStream;
  121. XmlizedString = UTF8ByteArrayToString(ms.ToArray());
  122. return XmlizedString;
  123. }
  124. private static String UTF8ByteArrayToString(Byte[] characters)
  125. {
  126. UTF8Encoding encoding = new UTF8Encoding();
  127. String constructedString = encoding.GetString(characters);
  128. if (constructedString.Length > 0)
  129. {
  130. constructedString = constructedString.Substring(1);
  131. }
  132. return (constructedString);
  133. }
  134. public static Object TextToObject(string inText, Type inType)
  135. {
  136. try
  137. {
  138. if (string.IsNullOrEmpty(inText))
  139. {
  140. return null;
  141. }
  142. else
  143. {
  144. XmlSerializer xs = new XmlSerializer(inType);
  145. MemoryStream ms = new MemoryStream(StringToUTF8ByteArray(inText));
  146. XmlTextWriter xmlTextWriter = new XmlTextWriter(ms, Encoding.UTF8);
  147. return (Object)xs.Deserialize(ms);
  148. }
  149. }
  150. catch
  151. {
  152. return null;
  153. }
  154. }
  155. private static byte[] StringToUTF8ByteArray(string inText)
  156. {
  157. UTF8Encoding encoding = new UTF8Encoding();
  158. Byte[] byteArray = encoding.GetBytes(inText);
  159. return byteArray;
  160. }
  161. }
  162. }