XML反序列化不起作用 - XML文档中存在错误(0,0)

时间:2016-12-23 09:34:27

标签: c# .net xml deserialization xmlserializer

我正在尝试反序列化XML响应,但我似乎无法理解这里没有用的是什么

这些是我的类(使用.NET开发人员工具生成)

[Serializable]
[XmlType(AnonymousType = true, Namespace = "http://www.crs.lombardia.it/schemas/DCSanita/GSSC/2007-01/calcolaIup/")]
[XmlRoot("GSSC.calcolaIupResponse", Namespace = "http://www.crs.lombardia.it/schemas/DCSanita/GSSC/2007-01/calcolaIup/", IsNullable = false)]
public partial class GSSCcalcolaIupResponse
{
    private param1 paramField;

    /// <remarks/>
    [XmlElementAttribute(Namespace = "")]
    public param1 param
    {
        get
        {
            return this.paramField;
        }
        set
        {
            this.paramField = value;
        }
    }
}

/// <remarks/>
[XmlType(AnonymousType = true)]
public partial class param1
{
    private string iupField;

    /// <remarks/>
    public string iup
    {
        get
        {
            return this.iupField;
        }
        set
        {
            this.iupField = value;
        }
    }
}

xml响应是这样的:

<m:GSSC.calcolaIupResponse xmlns:m="http://www.crs.lombardia.it/schemas/DCSanita/GSSC/2013-01/calcolaIup/">
    <param>
        <iup>0000W05RNF</iup>
    </param>
</m:GSSC.calcolaIupResponse>

这是反序列化的代码。

GSSCcalcolaIupResponse iup = new GSSCcalcolaIupResponse();
XmlSerializer serializer = new XmlSerializer(typeof(GSSCcalcolaIupResponse));

IUPDummyResponse retValue = new IUPDummyResponse();

using (TextReader reader = new StringReader(xml))
{
    if (soapFault == null)
        iup = (GSSCcalcolaIupResponse)serializer.Deserialize(reader);
}

1 个答案:

答案 0 :(得分:1)

GSSCcalcolaIupResponse中的命名空间和xml字符串中的命名空间是不同的。这是原因。只需更改为使用相同的命名空间,它将修复错误:

- http://www.crs.lombardia.it/schemas/DCSanita/GSSC/2007-01/calcolaIup/
- http://www.crs.lombardia.it/schemas/DCSanita/GSSC/2013-01/calcolaIup/