【问题标题】:Deserializing XML to C# Object returning null values将 XML 反序列化为 C# 对象返回空值
【发布时间】:2014-09-04 02:26:17
【问题描述】:

当尝试从来自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)); 

返回变量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文件是否包含所有数据
  • @Seminda XML 返回不存在 ListaMensagemRetorno

标签: c# asp.net .net xml


【解决方案1】:

问题

根据您的示例,这里的问题是您尝试反序列化的 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));
    ...
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    • 2022-01-14
    • 2016-12-16
    相关资源
    最近更新 更多