Neel 帮助反序列化这个 XML

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

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 &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 .

英文:

I have problems deserializing this XML.

&lt;?xml version=&#39;1.0&#39; encoding=&#39;UTF-8&#39;?&gt;
&lt;soapenv:Envelope xmlns:soapenv=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;&gt;
    &lt;soapenv:Body&gt;
        &lt;ns:getBasketResponse xmlns:ns=&quot;http://capi.basket.webservice.wg_base_app.enfinity.wg.com&quot;&gt;
            &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;
                &lt;ax27:costs xsi:type=&quot;ax27:Cost&quot;&gt;
                    &lt;ax27:name&gt;Verwerking (verpakking &amp;amp; levering)&lt;/ax27:name&gt;
                &lt;/ax27:costs&gt;
            &lt;/ns:return&gt;
        &lt;/ns:getBasketResponse&gt;
    &lt;/soapenv:Body&gt;
&lt;/soapenv:Envelope&gt;

These are my classes:

    public class GetBasketResponse
    {
        [XmlElement(&quot;ns:return&quot;)] public ReturnData ReturnData { get; set; }
    }
    public class ReturnData
    {
        [XmlElement(&quot;ax27:costs&quot;, Namespace= &quot;http://capi.webservice.wg_base_app.enfinity.wg.com/xsd&quot;)] 
        public List&lt;Cost&gt; Costs { get; set; }
    }
    public class Cost
    {
        [XmlElement(&quot;ax27:name&quot;, Namespace = &quot;http://capi.webservice.wg_base_app.enfinity.wg.com/xsd&quot;)]
        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 &lt;return xmlns='http://capi.basket.webservice.wg_base_app.enfinity.wg.com'&gt;.

答案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=&quot;costs&quot;, Namespace=&quot;http://capi.webservice.wg_base_app.enfinity.wg.com/xsd&quot;)]
public class Costs { 

	[XmlElement(ElementName=&quot;name&quot;, Namespace=&quot;http://capi.webservice.wg_base_app.enfinity.wg.com/xsd&quot;)] 
	public string Name; 

	[XmlAttribute(AttributeName=&quot;type&quot;, Namespace=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;)] 
	public string Type; 

	[XmlText] 
	public string Text; 
}

[XmlRoot(ElementName=&quot;return&quot;, Namespace=&quot;http://capi.basket.webservice.wg_base_app.enfinity.wg.com&quot;)]
public class Return { 

	[XmlElement(ElementName=&quot;costs&quot;, Namespace=&quot;http://capi.webservice.wg_base_app.enfinity.wg.com/xsd&quot;)] 
	public Costs Costs; 

	[XmlAttribute(AttributeName=&quot;ax27&quot;, Namespace=&quot;http://www.w3.org/2000/xmlns/&quot;)] 
	public string Ax27; 

	[XmlAttribute(AttributeName=&quot;xsi&quot;, Namespace=&quot;http://www.w3.org/2000/xmlns/&quot;)] 
	public string Xsi; 

	[XmlAttribute(AttributeName=&quot;type&quot;, Namespace=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;)] 
	public string Type; 

	[XmlText] 
	public string Text; 
}

[XmlRoot(ElementName=&quot;getBasketResponse&quot;, Namespace=&quot;http://capi.basket.webservice.wg_base_app.enfinity.wg.com&quot;)]
public class GetBasketResponse { 

	[XmlElement(ElementName=&quot;return&quot;, Namespace=&quot;http://capi.basket.webservice.wg_base_app.enfinity.wg.com&quot;)] 
	public Return Return; 

	[XmlAttribute(AttributeName=&quot;ns&quot;, Namespace=&quot;http://www.w3.org/2000/xmlns/&quot;)] 
	public string Ns; 

	[XmlText] 
	public string Text; 
}

[XmlRoot(ElementName=&quot;Body&quot;, Namespace=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;)]
public class Body { 

	[XmlElement(ElementName=&quot;getBasketResponse&quot;, Namespace=&quot;http://capi.basket.webservice.wg_base_app.enfinity.wg.com&quot;)] 
	public GetBasketResponse GetBasketResponse; 
}

[XmlRoot(ElementName=&quot;Envelope&quot;, Namespace=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;)]
public class Envelope { 

	[XmlElement(ElementName=&quot;Body&quot;, Namespace=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;)] 
	public Body Body; 

	[XmlAttribute(AttributeName=&quot;soapenv&quot;, Namespace=&quot;http://www.w3.org/2000/xmlns/&quot;)] 
	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 = &quot;Envelope&quot;, Namespace = &quot;http://schemas.xmlsoap.org/soap/envelope/&quot;)]
public class Envelope
{
[XmlElement(ElementName = &quot;Body&quot;, Namespace = &quot;http://schemas.xmlsoap.org/soap/envelope/&quot;)]
public Body Body { get; set; }
}
public class Body
{
[XmlElement(ElementName = &quot;getBasketResponse&quot;, Namespace = &quot;http://capi.basket.webservice.wg_base_app.enfinity.wg.com&quot;)]
public GetBasketResponse GetBasketResponse { get; set; }
}
public class GetBasketResponse
{
[XmlElement(ElementName = &quot;return&quot;, Namespace = &quot;http://capi.webservice.wg_base_app.enfinity.wg.com/xsd&quot;)]
public Response Response { get; set; }
}
[XmlRoot(ElementName = &quot;Response&quot;, Namespace = &quot;http://capi.webservice.wg_base_app.enfinity.wg.com/xsd&quot;)]
public class Response
{
[XmlElement(ElementName = &quot;costs&quot;, Namespace = &quot;http://capi.webservice.wg_base_app.enfinity.wg.com/xsd&quot;)]
public Cost Costs { get; set; }
}
public class Cost
{
[XmlElement(ElementName = &quot;name&quot;, Namespace = &quot;http://capi.webservice.wg_base_app.enfinity.wg.com/xsd&quot;)]
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;
}

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:

确定