如何阅读c#中的soap响应? [英] How to read soap response in c#?

查看:77
本文介绍了如何阅读c#中的soap响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

肥皂响应为:

Soap response as:

<?xml version="1.0" encoding="utf-8" ?> 
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<soap:Body>
		<LoginResponse xmlns="http://example.com/SystemIntegration">
  			<FirstName>@FirstName</FirstName>
			<LastName>@LastName</LastName>
		</LoginResponse>
    </soap:Body>
 </soap:Envelope>





我正在尝试将其读作:



I'm trying to read it as:

XDocument doc = XDocument.Parse(strReturnStatus);
List<XElement> result = doc.Elements("LoginResponse").ToList();
for (int intc = 0; intc <= result.Count - 1; intc++)
{
    strResponseCode = result[intc].Element("FirstName").Value.ToString();
    strResponseText = result[intc].Element("LastName").Value.ToString();
}





但它返回null结果。



如何在asp.net中读取以上内容c#??



But it returning null result.

How to read above respose in asp.net c#??

推荐答案

第一个问题: 元素方法仅选择指定节点的直接子节点。您需要使用 Descendants 方法。



第二个问题:节点你试图选择的不是LoginResponse;它有一个默认命名空间,它是元素名称的一部分。在检索元素时需要包含该命名空间。

First problem: The Elements method only selects direct children of the specified node. You need to use the Descendants method instead.

Second problem: The node you're trying to select is not called "LoginResponse"; it has a default namespace, which is part of the element name. You need to include that namespace when retrieving the elements.
XDocument doc = XDocument.Parse(strReturnStatus);
XNamespace ns = "http://example.com/SystemIntegration";
IEnumerable<XElement> responses = doc.Descendants(ns + "LoginResponse");
foreach (XElement response in responses)
{
    strResponseCode = (string)response.Element(ns + "FirstName");
    strResponseText = (string)response.Element(ns + "LastName");
}



XNamespace类(System.Xml.Linq) [ ^ ]


XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(strReturnStatus);

XmlNamespaceManager xmlnsManager = new System.Xml.XmlNamespaceManager(xmlDoc.NameTable);

xmlnsManager.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
xmlnsManager.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
xmlnsManager.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
xmlnsManager.AddNamespace("si", "http://example.com/SystemIntegration");

// You'd access the full path like this
XmlNode node = xmlDoc.SelectSingleNode("/soap:Envelope/soap:Body/si:LoginResponse/si:FirstName", xmlnsManager);
string firstname = node.InnerText;

// or just like this
node = xmlDoc.SelectSingleNode("//si:FirstName", xmlnsManager);
firstname = node.InnerText;


这篇关于如何阅读c#中的soap响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆