Neel 帮助反序列化这个 XML

huangapple go评论105阅读模式
英文:

Neel help to deserialize this XML

问题

I have problems deserializing this XML.

  1. <?xml version='1.0' encoding='UTF-8'?>
  2. <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  3. <soapenv:Body>
  4. <ns:getBasketResponse xmlns:ns="http://capi.basket.webservice.wg_base_app.enfinity.wg.com">
  5. <ns:return xmlns:ax27="http://capi.webservice.wg_base_app.enfinity.wg.com/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ax27:Response">
  6. <ax27:costs xsi:type="ax27:Cost">
  7. <ax27:name>Verwerking (verpakking &amp; levering)</ax27:name>
  8. </ax27:costs>
  9. </ns:return>
  10. </ns:getBasketResponse>
  11. </soapenv:Body>
  12. </soapenv:Envelope>

These are my classes:

  1. public class GetBasketResponse
  2. {
  3. [XmlElement("ns:return")]
  4. public ReturnData ReturnData { get; set; }
  5. }
  6. public class ReturnData
  7. {
  8. [XmlElement("ax27:costs", Namespace = "http://capi.webservice.wg_base_app.enfinity.wg.com/xsd")]
  9. public List<Cost> Costs { get; set; }
  10. }
  11. public class Cost
  12. {
  13. [XmlElement("ax27:name", Namespace = "http://capi.webservice.wg_base_app.enfinity.wg.com/xsd")]
  14. public string Name { get; set; }
  15. }

This is my code to deserialize:

  1. XmlSerializer serializer = new XmlSerializer(typeof(GetBasketResponse));
  2. using (StringReader reader = new StringReader(xml))
  3. {
  4. GetBasketResponse result = (GetBasketResponse)serializer.Deserialize(reader);
  5. }

Error: InvalidOperationException: The specified type was not recognized: name='Response', namespace='http://capi.webservice.wg_base_app.enfinity.wg.com/xsd', at .

英文:

I have problems deserializing this XML.

  1. &lt;?xml version=&#39;1.0&#39; encoding=&#39;UTF-8&#39;?&gt;
  2. &lt;soapenv:Envelope xmlns:soapenv=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;&gt;
  3. &lt;soapenv:Body&gt;
  4. &lt;ns:getBasketResponse xmlns:ns=&quot;http://capi.basket.webservice.wg_base_app.enfinity.wg.com&quot;&gt;
  5. &lt;ns:return xmlns:ax27=&quot;http://capi.webservice.wg_base_app.enfinity.wg.com/xsd&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:type=&quot;ax27:Response&quot;&gt;
  6. &lt;ax27:costs xsi:type=&quot;ax27:Cost&quot;&gt;
  7. &lt;ax27:name&gt;Verwerking (verpakking &amp;amp; levering)&lt;/ax27:name&gt;
  8. &lt;/ax27:costs&gt;
  9. &lt;/ns:return&gt;
  10. &lt;/ns:getBasketResponse&gt;
  11. &lt;/soapenv:Body&gt;
  12. &lt;/soapenv:Envelope&gt;

These are my classes:

  1. public class GetBasketResponse
  2. {
  3. [XmlElement(&quot;ns:return&quot;)] public ReturnData ReturnData { get; set; }
  4. }
  5. public class ReturnData
  6. {
  7. [XmlElement(&quot;ax27:costs&quot;, Namespace= &quot;http://capi.webservice.wg_base_app.enfinity.wg.com/xsd&quot;)]
  8. public List&lt;Cost&gt; Costs { get; set; }
  9. }
  10. public class Cost
  11. {
  12. [XmlElement(&quot;ax27:name&quot;, Namespace = &quot;http://capi.webservice.wg_base_app.enfinity.wg.com/xsd&quot;)]
  13. public string Name { get; set; }
  14. }

This is my code to deserialize:

  1. XmlSerializer serializer = new XmlSerializer(typeof(GetBasketResponse));
  2. using (StringReader reader = new StringReader(xml))
  3. {
  4. GetBasketResponse result = (GetBasketResponse)serializer.Deserialize(reader);
  5. }

Error: InvalidOperationException: The specified type was not recognized: name='Response', namespace='http://capi.webservice.wg_base_app.enfinity.wg.com/xsd', at &lt;return xmlns='http://capi.basket.webservice.wg_base_app.enfinity.wg.com'&gt;.

答案1

得分: 1

你应该使用工具来生成XML的正确类结构。例如,可以使用这个工具

对于提供的XML,工具生成以下类结构:

  1. [XmlRoot(ElementName="costs", Namespace="http://capi.webservice.wg_base_app.enfinity.wg.com/xsd")]
  2. public class Costs {
  3. [XmlElement(ElementName="name", Namespace="http://capi.webservice.wg_base_app.enfinity.wg.com/xsd")]
  4. public string Name;
  5. [XmlAttribute(AttributeName="type", Namespace="http://www.w3.org/2001/XMLSchema-instance")]
  6. public string Type;
  7. [XmlText]
  8. public string Text;
  9. }
  10. [XmlRoot(ElementName="return", Namespace="http://capi.basket.webservice.wg_base_app.enfinity.wg.com")]
  11. public class Return {
  12. [XmlElement(ElementName="costs", Namespace="http://capi.webservice.wg_base_app.enfinity.wg.com/xsd")]
  13. public Costs Costs;
  14. [XmlAttribute(AttributeName="ax27", Namespace="http://www.w3.org/2000/xmlns/")]
  15. public string Ax27;
  16. [XmlAttribute(AttributeName="xsi", Namespace="http://www.w3.org/2000/xmlns/")]
  17. public string Xsi;
  18. [XmlAttribute(AttributeName="type", Namespace="http://www.w3.org/2001/XMLSchema-instance")]
  19. public string Type;
  20. [XmlText]
  21. public string Text;
  22. }
  23. [XmlRoot(ElementName="getBasketResponse", Namespace="http://capi.basket.webservice.wg_base_app.enfinity.wg.com")]
  24. public class GetBasketResponse {
  25. [XmlElement(ElementName="return", Namespace="http://capi.basket.webservice.wg_base_app.enfinity.wg.com")]
  26. public Return Return;
  27. [XmlAttribute(AttributeName="ns", Namespace="http://www.w3.org/2000/xmlns/")]
  28. public string Ns;
  29. [XmlText]
  30. public string Text;
  31. }
  32. [XmlRoot(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
  33. public class Body {
  34. [XmlElement(ElementName="getBasketResponse", Namespace="http://capi.basket.webservice.wg_base_app.enfinity.wg.com")]
  35. public GetBasketResponse GetBasketResponse;
  36. }
  37. [XmlRoot(ElementName="Envelope", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
  38. public class Envelope {
  39. [XmlElement(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
  40. public Body Body;
  41. [XmlAttribute(AttributeName="soapenv", Namespace="http://www.w3.org/2000/xmlns/")]
  42. public string Soapenv;
  43. [XmlText]
  44. public string Text;
  45. }

然后,你可以像这样使用它:

  1. using System.Xml.Serialization;
  2. XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
  3. using (StringReader reader = new StringReader(xml))
  4. {
  5. var test = (Envelope)serializer.Deserialize(reader);
  6. }
英文:

You should use a tool to generate the correct class structure for your XML. For example, this one

For the provided XML, a tool generates this class structure

  1. [XmlRoot(ElementName=&quot;costs&quot;, Namespace=&quot;http://capi.webservice.wg_base_app.enfinity.wg.com/xsd&quot;)]
  2. public class Costs {
  3. [XmlElement(ElementName=&quot;name&quot;, Namespace=&quot;http://capi.webservice.wg_base_app.enfinity.wg.com/xsd&quot;)]
  4. public string Name;
  5. [XmlAttribute(AttributeName=&quot;type&quot;, Namespace=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;)]
  6. public string Type;
  7. [XmlText]
  8. public string Text;
  9. }
  10. [XmlRoot(ElementName=&quot;return&quot;, Namespace=&quot;http://capi.basket.webservice.wg_base_app.enfinity.wg.com&quot;)]
  11. public class Return {
  12. [XmlElement(ElementName=&quot;costs&quot;, Namespace=&quot;http://capi.webservice.wg_base_app.enfinity.wg.com/xsd&quot;)]
  13. public Costs Costs;
  14. [XmlAttribute(AttributeName=&quot;ax27&quot;, Namespace=&quot;http://www.w3.org/2000/xmlns/&quot;)]
  15. public string Ax27;
  16. [XmlAttribute(AttributeName=&quot;xsi&quot;, Namespace=&quot;http://www.w3.org/2000/xmlns/&quot;)]
  17. public string Xsi;
  18. [XmlAttribute(AttributeName=&quot;type&quot;, Namespace=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;)]
  19. public string Type;
  20. [XmlText]
  21. public string Text;
  22. }
  23. [XmlRoot(ElementName=&quot;getBasketResponse&quot;, Namespace=&quot;http://capi.basket.webservice.wg_base_app.enfinity.wg.com&quot;)]
  24. public class GetBasketResponse {
  25. [XmlElement(ElementName=&quot;return&quot;, Namespace=&quot;http://capi.basket.webservice.wg_base_app.enfinity.wg.com&quot;)]
  26. public Return Return;
  27. [XmlAttribute(AttributeName=&quot;ns&quot;, Namespace=&quot;http://www.w3.org/2000/xmlns/&quot;)]
  28. public string Ns;
  29. [XmlText]
  30. public string Text;
  31. }
  32. [XmlRoot(ElementName=&quot;Body&quot;, Namespace=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;)]
  33. public class Body {
  34. [XmlElement(ElementName=&quot;getBasketResponse&quot;, Namespace=&quot;http://capi.basket.webservice.wg_base_app.enfinity.wg.com&quot;)]
  35. public GetBasketResponse GetBasketResponse;
  36. }
  37. [XmlRoot(ElementName=&quot;Envelope&quot;, Namespace=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;)]
  38. public class Envelope {
  39. [XmlElement(ElementName=&quot;Body&quot;, Namespace=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;)]
  40. public Body Body;
  41. [XmlAttribute(AttributeName=&quot;soapenv&quot;, Namespace=&quot;http://www.w3.org/2000/xmlns/&quot;)]
  42. public string Soapenv;
  43. [XmlText]
  44. public string Text;
  45. }

Then you can use it like this

  1. using System.Xml.Serialization;
  2. XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
  3. using (StringReader reader = new StringReader(xml))
  4. {
  5. var test = (Envelope)serializer.Deserialize(reader);
  6. }

答案2

得分: 0

你可以像下面这样优化你的类:

  1. [XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
  2. public class Envelope
  3. {
  4. [XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
  5. public Body Body { get; set; }
  6. }
  7. public class Body
  8. {
  9. [XmlElement(ElementName = "getBasketResponse", Namespace = "http://capi.basket.webservice.wg_base_app.enfinity.wg.com")]
  10. public GetBasketResponse GetBasketResponse { get; set; }
  11. }
  12. public class GetBasketResponse
  13. {
  14. [XmlElement(ElementName = "return", Namespace = "http://capi.webservice.wg_base_app.enfinity.wg.com/xsd")]
  15. public Response Response { get; set; }
  16. }
  17. [XmlRoot(ElementName = "Response", Namespace = "http://capi.webservice.wg_base_app.enfinity.wg.com/xsd")]
  18. public class Response
  19. {
  20. [XmlElement(ElementName = "costs", Namespace = "http://capi.webservice.wg_base_app.enfinity.wg.com/xsd")]
  21. public Cost Costs { get; set; }
  22. }
  23. public class Cost
  24. {
  25. [XmlElement(ElementName = "name", Namespace = "http://capi.webservice.wg_base_app.enfinity.wg.com/xsd")]
  26. public string Name { get; set; }
  27. }

然后尝试使用以下代码完成任务:

  1. XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
  2. using (StringReader reader = new StringReader(xml))
  3. {
  4. Envelope envelope = (Envelope)serializer.Deserialize(reader);
  5. GetBasketResponse result = envelope.Body.GetBasketResponse;
  6. }
英文:

U can refine ur classes like below

  1. [XmlRoot(ElementName = &quot;Envelope&quot;, Namespace = &quot;http://schemas.xmlsoap.org/soap/envelope/&quot;)]
  2. public class Envelope
  3. {
  4. [XmlElement(ElementName = &quot;Body&quot;, Namespace = &quot;http://schemas.xmlsoap.org/soap/envelope/&quot;)]
  5. public Body Body { get; set; }
  6. }
  7. public class Body
  8. {
  9. [XmlElement(ElementName = &quot;getBasketResponse&quot;, Namespace = &quot;http://capi.basket.webservice.wg_base_app.enfinity.wg.com&quot;)]
  10. public GetBasketResponse GetBasketResponse { get; set; }
  11. }
  12. public class GetBasketResponse
  13. {
  14. [XmlElement(ElementName = &quot;return&quot;, Namespace = &quot;http://capi.webservice.wg_base_app.enfinity.wg.com/xsd&quot;)]
  15. public Response Response { get; set; }
  16. }
  17. [XmlRoot(ElementName = &quot;Response&quot;, Namespace = &quot;http://capi.webservice.wg_base_app.enfinity.wg.com/xsd&quot;)]
  18. public class Response
  19. {
  20. [XmlElement(ElementName = &quot;costs&quot;, Namespace = &quot;http://capi.webservice.wg_base_app.enfinity.wg.com/xsd&quot;)]
  21. public Cost Costs { get; set; }
  22. }
  23. public class Cost
  24. {
  25. [XmlElement(ElementName = &quot;name&quot;, Namespace = &quot;http://capi.webservice.wg_base_app.enfinity.wg.com/xsd&quot;)]
  26. public string Name { get; set; }
  27. }

Then try to use below code to get the job done

  1. XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
  2. using (StringReader reader = new StringReader(xml))
  3. {
  4. Envelope envelope = (Envelope)serializer.Deserialize(reader);
  5. GetBasketResponse result = envelope.Body.GetBasketResponse;
  6. }

huangapple
  • 本文由 发表于 2023年6月15日 02:21:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76476506.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定