c# - 将 XML 反序列化为返回空值的 C# 对象

标签 c# asp.net .net xml

当尝试从来自 WebService 的 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>";
            
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

enter image description here

编辑

据我所见,返回的不是 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 Namespace您尝试反序列化的 XML 字符串与反序列化对象本身之间的声明。

在 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. 或者...更改网络服务并将适当的 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));
    ...
    

关于c# - 将 XML 反序列化为返回空值的 C# 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25656404/

相关文章:

c# - 从标签 ID 更改整个 gridview 单元格颜色

c# - 如何在负载均衡器 (F5) 后面的 C# 中访问 MSMQ

c# - Windows Phone 7 中的语音搜索

c# - 为什么 visual studio .net 2008 添加 global.asax 文件后会失去调试能力?

c# - 作为资源的 URI

c# - .NET : Changing from Windows Authentication to Forms Authentication against AD

c# - 为什么 ? : cause a conversion error while if-else does not?

c# - EF 4.1 Code First - 添加列

asp.net - 面向完整 dotnet 框架的 ASP.NET Core Web 应用程序是否在 IIS 中工作?

c# - 确认消息框