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

本文关键字:xml 数组 反序列化 命名空间 | 更新日期: 2023-09-27 18:29:41

我需要反序列化这个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="Array",Namespace='http://schemas.xmlsoap.org/soap/encoding/',位于。

但是如果我添加名称空间"http://schemas.xmlsoap.org/soap/encoding/"对于estraiListaAttiFacicoloResponse类的@return属性,异常消失,但无论如何都不要反序列化数组项

有人能帮我吗?

感谢

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

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

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