使用命名空间反序列化xml数组

时间:2014-11-09 10:44:52

标签: c# xml soap

我需要反序列化这个xml

<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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">
    <SOAP-ENV:Body>
        <ns1:estraiListaAttiFascicoloResponse xmlns:ns1="urn:BEAFascicoloInformatico-distr">
            <return xmlns:ns2="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns2:Array" ns2:arrayType="ns1:BEADocumentoFascicoloVO[2]">
                <item xsi:type="ns1:BEADocumentoFascicoloVO">
                    <annoFascicolo xsi:type="xsd:string">1998</annoFascicolo>
                </item>
                <item xsi:type="ns1:BEADocumentoFascicoloVO">
                    <annoFascicolo xsi:type="xsd:string">1998</annoFascicolo>
                </item>
            </return>
        </ns1:estraiListaAttiFascicoloResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我尝试过这个课程:

[XmlType(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
[XmlRoot(Namespace = "http://schemas.xmlsoap.org/soap/envelope/", IsNullable = false)]
public class Envelope
{
    public EnvelopeBody Body { get; set; }

    [XmlAttribute(Form = XmlSchemaForm.Qualified)]
    public string encodingStyle { get; set; }
}

[XmlType(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class EnvelopeBody
{
    [XmlElement(Namespace = "urn:BEAFascicoloInformatico-distr")]
    public estraiListaAttiFascicoloResponse estraiListaAttiFascicoloResponse { get; set; }
}

/// <remarks/>
[XmlType(AnonymousType = true, Namespace = "urn:BEAFascicoloInformatico-distr")]
[XmlRoot(Namespace = "urn:BEAFascicoloInformatico-distr", IsNullable = false)]
public class estraiListaAttiFascicoloResponse
{
    [XmlElement(Namespace = "")]
    public @return @return { get; set; }
}

[XmlType(AnonymousType = true)]
[XmlRoot(Namespace = "", IsNullable = false)]
public class @return
{
    [XmlElement("item")]
    public returnItem[] item { get; set; }

    [XmlAttribute(Form = XmlSchemaForm.Qualified, Namespace = "http://schemas.xmlsoap.org/soap/encoding/")]
    public string arrayType { get; set; }
}

[XmlType(AnonymousType = true)]
public class returnItem
{
    public ushort annoFascicolo { get; set; }
}

和此代码

var serializer = new XmlSerializer(typeof(Envelope));

var sw = new StringReader(xmlText);

using (var xmlTextReader = XmlReader.Create(sw))
{
   var converted = serializer.Deserialize(xmlTextReader);
}

但我在运行时收到此错误

其他信息:XML文档中存在错误(5,5)。

和此

中的内部异常

无法识别指定的类型:name =&#39; Array&#39;,Namespace =&#39; http://schemas.xmlsoap.org/soap/encoding/&#39;,at。

但如果我添加命名空间&#34; http://schemas.xmlsoap.org/soap/encoding/&#34;到estraiListaAttiFascicoloResponse类的@return属性异常消失,但不要反序列化数组项目

有人可以帮助我吗?

由于

1 个答案:

答案 0 :(得分:0)

我没有看到名称空间ns1的解析。

您还可以使用免费的开源工具XML Notepad检查xml的有效性,并从Microsoft下载。