对C#类的肥皂响应始终返回空值

卡尔图斯汗

我很难解析来自Web服务的soap响应并将其转换为c#对象。我所做的总是返回null元素。这是我的回应。

<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' 
 xmlns:tem='http://tempuri.org/'>
 <soapenv:Header/> <soapenv:Body>
 <tem:Response><tem:Result>
  <RETN>108</RETN><DESC> This is an error</DESC></tem:Result></tem:Response>
   </soapenv:Body>
 </soapenv:Envelope>

这就是我解析此响应的方式。

var xDoc = XDocument.Parse(response);
var xLoginResult = xDoc.Root.Descendants().FirstOrDefault(d => d.Name.LocalName.Equals("Result"));
var serializer = new XmlSerializer(typeof(Result));
using (var reader = xLoginResult.CreateReader())
{
    var convertedResonse= (Result)serializer.Deserialize(reader);
    // inside this convertedResonse RETN and  DESC is alway null
}

这是我的结果类

   [XmlRoot(ElementName = "Result", Namespace = "http://tempuri.org/")]
    public class Result
    {
        [XmlElement("RETN")]
        public string RETN { get; set; }
        [XmlElement("DESC")]
        public string DESC { get; set; }
    }

    [XmlRoot(ElementName = "Response", Namespace = "http://tempuri.org/")]
    public class Response
    {
        [XmlElement(ElementName = "Result", Namespace = "http://tempuri.org/")]
        public Result Result { get; set; }
    }

    [XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Body
    {
        [XmlElement(ElementName = "Response", Namespace = "http://tempuri.org/")]
        public Response Response { get; set; }
    }

    [XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Envelope
    {
        [XmlElement(ElementName = "Header", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public string Header { get; set; }
        [XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public Body Body { get; set; }
        [XmlAttribute(AttributeName = "soapenv", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Soapenv { get; set; }
        [XmlAttribute(AttributeName = "tem", Namespace = "http://www.w3.org/2000/xmlns/")]
        public string Tem { get; set; }
    }

内部的convertedResonse值始终为null。任何想法将不胜感激?

萨吉德

对于问题中发布Xml,您不需要使用XmlSerializer,只需XDocument使用Linq到Xml就足够了,例如以下代码:

1-创建命名空间:

XNamespace xn = "http://tempuri.org/";

2-将XDocument的查询修改为:

Result result = xDoc
    .Descendants(xn + "Result")
    .Select(x => new Result { DESC = x.Element("DESC").Value, RETN = x.Element("RETN").Value })
    .FirstOrDefault();

Console.WriteLine($"DESC:{result.DESC}, RETN:{result.RETN}");

结果

DESC: This is an error, RETN:108

我希望你觉得这有帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Bing Visual Search C# SDK 返回空响应

来自分类Dev

字典类返回空值

来自分类Dev

Mvvmcross逻辑删除-始终返回空值

来自分类Dev

肥皂服务方法类定义C#

来自分类Dev

在C#中解析JSON返回空值

来自分类Dev

肥皂响应的返回序列类型

来自分类Dev

GetMethod 返回空 c#

来自分类Dev

C#-数据绑定组合框始终为.selectedItem或.selectedValue返回空值

来自分类Dev

URLSearchParams始终返回空

来自分类Dev

在C#中从C ++ .DLL加载值始终返回true

来自分类Dev

Singleton Java类返回空值

来自分类Dev

Ajax响应返回空值。网络响应显示价值

来自分类Dev

肥皂响应被截断(SoapUi返回正确的响应)

来自分类Dev

使用点作为密钥哈希表始终返回空值

来自分类Dev

GWT-History.getToken()始终返回空白值

来自分类Dev

将肥皂XML解析为C#类

来自分类Dev

askWithCarousel() 返回空响应

来自分类Dev

邮递员在SOAP响应中返回空值

来自分类Dev

ZipArchive返回空文件C#

来自分类Dev

c# 中的 FLWOR XQuery 返回空?

来自分类Dev

codeigniter缓存始终返回空

来自分类Dev

GetRoomList()始终返回空数组

来自分类Dev

C#JsonConvertDeserialization返回空值

来自分类Dev

为什么在C#中反序列化我的packages.config返回空值?

来自分类Dev

在C#中反序列化嵌套数组JSON返回空值

来自分类Dev

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

来自分类Dev

c#无法与firebase连接以及SetTaskAsync为什么使用当前代码返回空值

来自分类Dev

类返回空列表

来自分类Dev

Redux 存储在 React 类组件中返回空值