繁体   English   中英

将XML反序列化为C#对象,返回空值

[英]Deserializing XML to C# Object returning null values

当尝试从来自WebService的XML文本中获取值时,这些值将为null,如下图所示。

string texto = 
    "<?xml version=\"1.0\"?>" + 
    "<EnviarLoteRpsResposta xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"+
    "   <NumeroLote>3774</NumeroLote>" +
    "   <DataRecebimento>2014-09-03T23:03:57.3428675-03:00</DataRecebimento>" +
    "   <Protocolo>635453822373428675</Protocolo>" +
    "</EnviarLoteRpsResposta>";

XmlRootAttribute rootAttribute = new XmlRootAttribute("EnviarLoteRpsResposta");
XmlSerializer serializer = new XmlSerializer(typeof(WS.NF.EnviarLoteRpsResposta), rootAttribute);
WS.NF.EnviarLoteRpsResposta ei = (WS.NF.EnviarLoteRpsResposta)serializer.Deserialize(new StringReader(texto)); 

返回variabble ei

在此处输入图片说明

编辑

从我看到的结果来看,返回的不是ListaMensagemRetorno字段。 这是问题吗?

服务参考

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.e-governeapps2.com.br/")]
public partial class EnviarLoteRpsResposta {

    private System.Nullable<ulong> numeroLoteField;

    private System.Nullable<System.DateTime> dataRecebimentoField;

    private string protocoloField;

    private MensagemRetorno[] listaMensagemRetornoField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
    public System.Nullable<ulong> NumeroLote {
        get {
            return this.numeroLoteField;
        }
        set {
            this.numeroLoteField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
    public System.Nullable<System.DateTime> DataRecebimento {
        get {
            return this.dataRecebimentoField;
        }
        set {
            this.dataRecebimentoField = value;
        }
    }

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

    /// <remarks/>
    public MensagemRetorno[] ListaMensagemRetorno {
        get {
            return this.listaMensagemRetornoField;
        }
        set {
            this.listaMensagemRetornoField = value;
        }
    }
}

问题

根据您的示例,这里的问题是,您要反序列化的XML字符串与反序列化的对象本身之间具有不同的XML Namespace声明。

在XML字符串中,XML没有EnviarLoteRpsResposta的XML命名空间声明(无默认命名空间):

string texto = 
    "<?xml version=\"1.0\"?>" + 
    "<EnviarLoteRpsResposta xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"+
    "   <NumeroLote>3774</NumeroLote>" +
    "   <DataRecebimento>2014-09-03T23:03:57.3428675-03:00</DataRecebimento>" +
    "   <Protocolo>635453822373428675</Protocolo>" +
    "</EnviarLoteRpsResposta>";

在您的EnviarLoteRpsResposta类中,XML命名空间声明为http://www.e-governeapps2.com.br/

...
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.e-governeapps2.com.br/")]
public partial class EnviarLoteRpsResposta { ... }  


解决方法

为了使反序列化的工作,你需要做以下之一

  1. 更改EnviarLoteRpsResposta类并删除XML命名空间声明:

     ... /* REMOVED [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.e-governeapps2.com.br/")] */ public partial class EnviarLoteRpsResposta { ... } 
  2. 或者...更改Web服务,并将适当的XML命名空间添加到返回的XML字符串中:

     string texto = "<?xml version=\\"1.0\\"?>" + "<EnviarLoteRpsResposta xmlns=\\"http://www.e-governeapps2.com.br/\\" xmlns:xsd=\\"http://www.w3.org/2001/XMLSchema\\" xmlns:xsi=\\"http://www.w3.org/2001/XMLSchema-instance\\">"+ " <NumeroLote>3774</NumeroLote>" + " <DataRecebimento>2014-09-03T23:03:57.3428675-03:00</DataRecebimento>" + " <Protocolo>635453822373428675</Protocolo>" + "</EnviarLoteRpsResposta>"; 

    然后稍微更改代码以反序列化XML字符串为:

      ... XmlRootAttribute rootAttribute = new XmlRootAttribute("EnviarLoteRpsResposta") { Namespace = "http://www.e-governeapps2.com.br/" }; ... 
  3. 或...更改用于反序列化XML字符串的代码,并以编程方式添加适当的XML命名空间(对EnviarLoteRpsResposta类或Web服务无更改):

     ... NameTable nt = new NameTable(); XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt); nsmgr.AddNamespace(String.Empty, "http://www.e-governeapps2.com.br/"); XmlParserContext context = new XmlParserContext(nt, nsmgr, null, XmlSpace.None); XmlRootAttribute rootAttribute = new XmlRootAttribute("EnviarLoteRpsResposta") { Namespace = "http://www.e-governeapps2.com.br/" }; XmlSerializer serializer = new XmlSerializer(typeof(WS.NF.EnviarLoteRpsResposta), rootAttribute); WS.NF.EnviarLoteRpsResposta ei = (WS.NF.EnviarLoteRpsResposta)serializer.Deserialize(new XmlTextReader(texto, XmlNodeType.Element, context)); ... 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM