自动创建的xml反序列化的C#类不起作用

前端之家收集整理的这篇文章主要介绍了自动创建的xml反序列化的C#类不起作用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在努力为此xml创建反序列化类:
  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:stn="urn:response">
  3. <SOAP-ENV:Body>
  4. <Response>
  5. <Records xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="stn:Record[1]">
  6. <item xsi:type="stn:Record">
  7. <person xsi:type="xsd:string">John Johnos</person>
  8. <address xsi:type="xsd:string">Some Street 1</address>
  9. <age xsi:type="xsd:string">24</age>
  10. </item>
  11. </Records>
  12. <status xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="stn:status[1]">
  13. <item xsi:type="stn:status">
  14. <status xsi:type="xsd:string">success</status>
  15. <message xsi:type="xsd:string"/>
  16. </item>
  17. </status>
  18. </Response>
  19. </SOAP-ENV:Body>
  20. </SOAP-ENV:Envelope>

我试图使用自动创建的代码(在VisualStudio 12:编辑 – >粘贴特殊 – >将XML粘贴为类):

  1. /// <remarks/>
  2. [System.SerializableAttribute()]
  3. [System.ComponentModel.DesignerCategoryAttribute("code")]
  4. [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true,Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
  5. [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://schemas.xmlsoap.org/soap/envelope/",IsNullable = false)]
  6. public partial class Envelope
  7. {
  8.  
  9. private EnvelopeBody bodyField;
  10.  
  11. private string encodingStyleField;
  12.  
  13. /// <remarks/>
  14. public EnvelopeBody Body
  15. {
  16. get
  17. {
  18. return this.bodyField;
  19. }
  20. set
  21. {
  22. this.bodyField = value;
  23. }
  24. }
  25.  
  26. /// <remarks/>
  27. [System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified)]
  28. public string encodingStyle
  29. {
  30. get
  31. {
  32. return this.encodingStyleField;
  33. }
  34. set
  35. {
  36. this.encodingStyleField = value;
  37. }
  38. }
  39. }
  40.  
  41. /// <remarks/>
  42. [System.SerializableAttribute()]
  43. [System.ComponentModel.DesignerCategoryAttribute("code")]
  44. [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true,Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
  45. public partial class EnvelopeBody
  46. {
  47.  
  48. private Response responseField;
  49.  
  50. /// <remarks/>
  51. [System.Xml.Serialization.XmlElementAttribute(Namespace = "")]
  52. public Response Response
  53. {
  54. get
  55. {
  56. return this.responseField;
  57. }
  58. set
  59. {
  60. this.responseField = value;
  61. }
  62. }
  63. }
  64.  
  65. /// <remarks/>
  66. [System.SerializableAttribute()]
  67. [System.ComponentModel.DesignerCategoryAttribute("code")]
  68. [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
  69. [System.Xml.Serialization.XmlRootAttribute(Namespace = "",IsNullable = false)]
  70. public partial class Response
  71. {
  72.  
  73. private ResponseRecords recordsField;
  74.  
  75. private ResponseStatus statusField;
  76.  
  77. /// <remarks/>
  78. public ResponseRecords Records
  79. {
  80. get
  81. {
  82. return this.recordsField;
  83. }
  84. set
  85. {
  86. this.recordsField = value;
  87. }
  88. }
  89.  
  90. /// <remarks/>
  91. public ResponseStatus status
  92. {
  93. get
  94. {
  95. return this.statusField;
  96. }
  97. set
  98. {
  99. this.statusField = value;
  100. }
  101. }
  102. }
  103.  
  104. /// <remarks/>
  105. [System.SerializableAttribute()]
  106. [System.ComponentModel.DesignerCategoryAttribute("code")]
  107. [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
  108. public partial class ResponseRecords
  109. {
  110.  
  111. private ResponseRecordsItem itemField;
  112.  
  113. private string arrayTypeField;
  114.  
  115. /// <remarks/>
  116. public ResponseRecordsItem item
  117. {
  118. get
  119. {
  120. return this.itemField;
  121. }
  122. set
  123. {
  124. this.itemField = value;
  125. }
  126. }
  127.  
  128. /// <remarks/>
  129. [System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified,Namespace = "http://schemas.xmlsoap.org/soap/encoding/")]
  130. public string arrayType
  131. {
  132. get
  133. {
  134. return this.arrayTypeField;
  135. }
  136. set
  137. {
  138. this.arrayTypeField = value;
  139. }
  140. }
  141. }
  142.  
  143. /// <remarks/>
  144. [System.SerializableAttribute()]
  145. [System.ComponentModel.DesignerCategoryAttribute("code")]
  146. [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
  147. public partial class ResponseRecordsItem
  148. {
  149.  
  150. private string personField;
  151.  
  152. private string addressField;
  153.  
  154. private byte ageField;
  155.  
  156. /// <remarks/>
  157. public string person
  158. {
  159. get
  160. {
  161. return this.personField;
  162. }
  163. set
  164. {
  165. this.personField = value;
  166. }
  167. }
  168.  
  169. /// <remarks/>
  170. public string address
  171. {
  172. get
  173. {
  174. return this.addressField;
  175. }
  176. set
  177. {
  178. this.addressField = value;
  179. }
  180. }
  181.  
  182. /// <remarks/>
  183. public byte age
  184. {
  185. get
  186. {
  187. return this.ageField;
  188. }
  189. set
  190. {
  191. this.ageField = value;
  192. }
  193. }
  194. }
  195.  
  196. /// <remarks/>
  197. [System.SerializableAttribute()]
  198. [System.ComponentModel.DesignerCategoryAttribute("code")]
  199. [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
  200. public partial class ResponseStatus
  201. {
  202.  
  203. private ResponseStatusItem itemField;
  204.  
  205. private string arrayTypeField;
  206.  
  207. /// <remarks/>
  208. public ResponseStatusItem item
  209. {
  210. get
  211. {
  212. return this.itemField;
  213. }
  214. set
  215. {
  216. this.itemField = value;
  217. }
  218. }
  219.  
  220. /// <remarks/>
  221. [System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified,Namespace = "http://schemas.xmlsoap.org/soap/encoding/")]
  222. public string arrayType
  223. {
  224. get
  225. {
  226. return this.arrayTypeField;
  227. }
  228. set
  229. {
  230. this.arrayTypeField = value;
  231. }
  232. }
  233. }
  234.  
  235. /// <remarks/>
  236. [System.SerializableAttribute()]
  237. [System.ComponentModel.DesignerCategoryAttribute("code")]
  238. [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
  239. public partial class ResponseStatusItem
  240. {
  241.  
  242. private string statusField;
  243.  
  244. private object messageField;
  245.  
  246. /// <remarks/>
  247. public string status
  248. {
  249. get
  250. {
  251. return this.statusField;
  252. }
  253. set
  254. {
  255. this.statusField = value;
  256. }
  257. }
  258.  
  259. /// <remarks/>
  260. public object message
  261. {
  262. get
  263. {
  264. return this.messageField;
  265. }
  266. set
  267. {
  268. this.messageField = value;
  269. }
  270. }
  271. }

我试图在XMLSerializer的帮助下反序列化:

  1. var serializer = new XmlSerializer(typeof(Envelope));
  2. var reader = new StringReader(response);
  3. var flResponse = (Envelope)serializer.Deserialize(reader);

我得到的错误消息:

  1. Message=The specified type was not recognized: name='Array',namespace='http://schemas.xmlsoap.org/soap/encoding/',at <Records xmlns=''>.

你能请我帮忙为这个xml挑选反序列化类吗?

我尝试了很多东西,然后弄清楚了.您发布的Xml无效,因为xsi:type在反序列化中不起作用.

有效的XML应该如下所示:

  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:stn="urn:response">
  3. <SOAP-ENV:Body>
  4. <Response>
  5. <Records>
  6. <item>
  7. <person >John Johnos</person>
  8. <address >Some Street 1</address>
  9. <age >24</age>
  10. </item>
  11. </Records>
  12. <status>
  13. <item>
  14. <status >success</status>
  15. <message/>
  16. </item>
  17. </status>
  18. </Response>
  19. </SOAP-ENV:Body>
  20. </SOAP-ENV:Envelope>

代码应该看起来像:

  1. XDocument xml = XDocument.Parse(xmlInput);
  2.  
  3. XmlSerializer serializer = new XmlSerializer(typeof(Response));
  4.  
  5. using (StringReader stream = new StringReader(items[0].ToString()))
  6. {
  7. var output = (Response)serializer.Deserialize(stream);
  8. }

自动生成类将来自:

  1. <Response>
  2. <Records xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  3. <item>
  4. <person>John Johnos</person>
  5. <address >Some Street 1</address>
  6. <age>24</age>
  7. </item>
  8. </Records>
  9. <status xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  10. <item >
  11. <status >success</status>
  12. <message />
  13. </item>
  14. </status>
  15. </Response>

希望这很清楚.不知道如何摆脱Envelope的类型,所以这可能不是你想要的解决方案.

用于从Envelope获取东西的方法是XDocument.Descendants(elemmentName),它返回数组或该名称的元素列表,然后可以填充对象.它更多的工作,但我认为它比将XML转换为反序列化更好.

猜你在找的XML相关文章