英文:
Neel help to deserialize this XML
问题
I have problems deserializing this XML.
<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns:getBasketResponse xmlns:ns="http://capi.basket.webservice.wg_base_app.enfinity.wg.com">
<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">
<ax27:costs xsi:type="ax27:Cost">
<ax27:name>Verwerking (verpakking & levering)</ax27:name>
</ax27:costs>
</ns:return>
</ns:getBasketResponse>
</soapenv:Body>
</soapenv:Envelope>
These are my classes:
public class GetBasketResponse
{
[XmlElement("ns:return")]
public ReturnData ReturnData { get; set; }
}
public class ReturnData
{
[XmlElement("ax27:costs", Namespace = "http://capi.webservice.wg_base_app.enfinity.wg.com/xsd")]
public List<Cost> Costs { get; set; }
}
public class Cost
{
[XmlElement("ax27:name", Namespace = "http://capi.webservice.wg_base_app.enfinity.wg.com/xsd")]
public string Name { get; set; }
}
This is my code to deserialize:
XmlSerializer serializer = new XmlSerializer(typeof(GetBasketResponse));
using (StringReader reader = new StringReader(xml))
{
GetBasketResponse result = (GetBasketResponse)serializer.Deserialize(reader);
}
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.
<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns:getBasketResponse xmlns:ns="http://capi.basket.webservice.wg_base_app.enfinity.wg.com">
<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">
<ax27:costs xsi:type="ax27:Cost">
<ax27:name>Verwerking (verpakking &amp; levering)</ax27:name>
</ax27:costs>
</ns:return>
</ns:getBasketResponse>
</soapenv:Body>
</soapenv:Envelope>
These are my classes:
public class GetBasketResponse
{
[XmlElement("ns:return")] public ReturnData ReturnData { get; set; }
}
public class ReturnData
{
[XmlElement("ax27:costs", Namespace= "http://capi.webservice.wg_base_app.enfinity.wg.com/xsd")]
public List<Cost> Costs { get; set; }
}
public class Cost
{
[XmlElement("ax27:name", Namespace = "http://capi.webservice.wg_base_app.enfinity.wg.com/xsd")]
public string Name { get; set; }
}
This is my code to deserialize:
XmlSerializer serializer = new XmlSerializer(typeof(GetBasketResponse));
using (StringReader reader = new StringReader(xml))
{
GetBasketResponse result = (GetBasketResponse)serializer.Deserialize(reader);
}
Error: InvalidOperationException: The specified type was not recognized: name='Response', namespace='http://capi.webservice.wg_base_app.enfinity.wg.com/xsd', at <return xmlns='http://capi.basket.webservice.wg_base_app.enfinity.wg.com'>.
答案1
得分: 1
你应该使用工具来生成XML的正确类结构。例如,可以使用这个工具。
对于提供的XML,工具生成以下类结构:
[XmlRoot(ElementName="costs", Namespace="http://capi.webservice.wg_base_app.enfinity.wg.com/xsd")]
public class Costs {
[XmlElement(ElementName="name", Namespace="http://capi.webservice.wg_base_app.enfinity.wg.com/xsd")]
public string Name;
[XmlAttribute(AttributeName="type", Namespace="http://www.w3.org/2001/XMLSchema-instance")]
public string Type;
[XmlText]
public string Text;
}
[XmlRoot(ElementName="return", Namespace="http://capi.basket.webservice.wg_base_app.enfinity.wg.com")]
public class Return {
[XmlElement(ElementName="costs", Namespace="http://capi.webservice.wg_base_app.enfinity.wg.com/xsd")]
public Costs Costs;
[XmlAttribute(AttributeName="ax27", Namespace="http://www.w3.org/2000/xmlns/")]
public string Ax27;
[XmlAttribute(AttributeName="xsi", Namespace="http://www.w3.org/2000/xmlns/")]
public string Xsi;
[XmlAttribute(AttributeName="type", Namespace="http://www.w3.org/2001/XMLSchema-instance")]
public string Type;
[XmlText]
public string Text;
}
[XmlRoot(ElementName="getBasketResponse", Namespace="http://capi.basket.webservice.wg_base_app.enfinity.wg.com")]
public class GetBasketResponse {
[XmlElement(ElementName="return", Namespace="http://capi.basket.webservice.wg_base_app.enfinity.wg.com")]
public Return Return;
[XmlAttribute(AttributeName="ns", Namespace="http://www.w3.org/2000/xmlns/")]
public string Ns;
[XmlText]
public string Text;
}
[XmlRoot(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public class Body {
[XmlElement(ElementName="getBasketResponse", Namespace="http://capi.basket.webservice.wg_base_app.enfinity.wg.com")]
public GetBasketResponse GetBasketResponse;
}
[XmlRoot(ElementName="Envelope", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope {
[XmlElement(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public Body Body;
[XmlAttribute(AttributeName="soapenv", Namespace="http://www.w3.org/2000/xmlns/")]
public string Soapenv;
[XmlText]
public string Text;
}
然后,你可以像这样使用它:
using System.Xml.Serialization;
XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
using (StringReader reader = new StringReader(xml))
{
var test = (Envelope)serializer.Deserialize(reader);
}
英文:
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
[XmlRoot(ElementName="costs", Namespace="http://capi.webservice.wg_base_app.enfinity.wg.com/xsd")]
public class Costs {
[XmlElement(ElementName="name", Namespace="http://capi.webservice.wg_base_app.enfinity.wg.com/xsd")]
public string Name;
[XmlAttribute(AttributeName="type", Namespace="http://www.w3.org/2001/XMLSchema-instance")]
public string Type;
[XmlText]
public string Text;
}
[XmlRoot(ElementName="return", Namespace="http://capi.basket.webservice.wg_base_app.enfinity.wg.com")]
public class Return {
[XmlElement(ElementName="costs", Namespace="http://capi.webservice.wg_base_app.enfinity.wg.com/xsd")]
public Costs Costs;
[XmlAttribute(AttributeName="ax27", Namespace="http://www.w3.org/2000/xmlns/")]
public string Ax27;
[XmlAttribute(AttributeName="xsi", Namespace="http://www.w3.org/2000/xmlns/")]
public string Xsi;
[XmlAttribute(AttributeName="type", Namespace="http://www.w3.org/2001/XMLSchema-instance")]
public string Type;
[XmlText]
public string Text;
}
[XmlRoot(ElementName="getBasketResponse", Namespace="http://capi.basket.webservice.wg_base_app.enfinity.wg.com")]
public class GetBasketResponse {
[XmlElement(ElementName="return", Namespace="http://capi.basket.webservice.wg_base_app.enfinity.wg.com")]
public Return Return;
[XmlAttribute(AttributeName="ns", Namespace="http://www.w3.org/2000/xmlns/")]
public string Ns;
[XmlText]
public string Text;
}
[XmlRoot(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public class Body {
[XmlElement(ElementName="getBasketResponse", Namespace="http://capi.basket.webservice.wg_base_app.enfinity.wg.com")]
public GetBasketResponse GetBasketResponse;
}
[XmlRoot(ElementName="Envelope", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope {
[XmlElement(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
public Body Body;
[XmlAttribute(AttributeName="soapenv", Namespace="http://www.w3.org/2000/xmlns/")]
public string Soapenv;
[XmlText]
public string Text;
}
Then you can use it like this
using System.Xml.Serialization;
XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
using (StringReader reader = new StringReader(xml))
{
var test = (Envelope)serializer.Deserialize(reader);
}
答案2
得分: 0
你可以像下面这样优化你的类:
[XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope
{
[XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public Body Body { get; set; }
}
public class Body
{
[XmlElement(ElementName = "getBasketResponse", Namespace = "http://capi.basket.webservice.wg_base_app.enfinity.wg.com")]
public GetBasketResponse GetBasketResponse { get; set; }
}
public class GetBasketResponse
{
[XmlElement(ElementName = "return", Namespace = "http://capi.webservice.wg_base_app.enfinity.wg.com/xsd")]
public Response Response { get; set; }
}
[XmlRoot(ElementName = "Response", Namespace = "http://capi.webservice.wg_base_app.enfinity.wg.com/xsd")]
public class Response
{
[XmlElement(ElementName = "costs", Namespace = "http://capi.webservice.wg_base_app.enfinity.wg.com/xsd")]
public Cost Costs { get; set; }
}
public class Cost
{
[XmlElement(ElementName = "name", Namespace = "http://capi.webservice.wg_base_app.enfinity.wg.com/xsd")]
public string Name { get; set; }
}
然后尝试使用以下代码完成任务:
XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
using (StringReader reader = new StringReader(xml))
{
Envelope envelope = (Envelope)serializer.Deserialize(reader);
GetBasketResponse result = envelope.Body.GetBasketResponse;
}
英文:
U can refine ur classes like below
[XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope
{
[XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public Body Body { get; set; }
}
public class Body
{
[XmlElement(ElementName = "getBasketResponse", Namespace = "http://capi.basket.webservice.wg_base_app.enfinity.wg.com")]
public GetBasketResponse GetBasketResponse { get; set; }
}
public class GetBasketResponse
{
[XmlElement(ElementName = "return", Namespace = "http://capi.webservice.wg_base_app.enfinity.wg.com/xsd")]
public Response Response { get; set; }
}
[XmlRoot(ElementName = "Response", Namespace = "http://capi.webservice.wg_base_app.enfinity.wg.com/xsd")]
public class Response
{
[XmlElement(ElementName = "costs", Namespace = "http://capi.webservice.wg_base_app.enfinity.wg.com/xsd")]
public Cost Costs { get; set; }
}
public class Cost
{
[XmlElement(ElementName = "name", Namespace = "http://capi.webservice.wg_base_app.enfinity.wg.com/xsd")]
public string Name { get; set; }
}
Then try to use below code to get the job done
XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
using (StringReader reader = new StringReader(xml))
{
Envelope envelope = (Envelope)serializer.Deserialize(reader);
GetBasketResponse result = envelope.Body.GetBasketResponse;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论