c# - SOAP XML 消息
问题描述
我试图阅读这个 SOAP XML 消息
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<EchoSoapRequest xmlns="http://tempuri.org/">
<grantapplication externalsystemreference="0743A61C-B3F8-4B51-AF1E-FBE76172D34C" externalid="d77ddae7-ad19-4c4a-b3bf-1e83df82e40f">
<scheme>CDD</scheme>
<applicationdate> 20170126 </applicationdate>
<category> CDD F </category>
<applicant>
<title>Mr</title>
</applicant>
</grantapplication>
</EchoSoapRequest>
</soap:Body>
</soap:Envelope>
这是我的方法
public bool SaveContacts(XmlDocument application)
{
XDocument xmessage = XDocument.Parse(application.OuterXml);
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";//Envelop namespace s
XNamespace xsd = "http://www.w3.org/2001/XMLSchema";//Envelop namespace s
XNamespace soap = "http://schemas.xmlsoap.org/soap/envelope/";//Envelop namespace s
XNamespace d = "http://tempuri.org/";//bookHotelResponse namespace
XNamespace externalsystemreference = "0743A61C-B3F8-4B51-AF1E-FBE76172D34C";//d namespace
XNamespace externalid = "d77ddae7-ad19-4c4a-b3bf-1e83df82e40f";//d namespace
foreach (var itm in xmessage.Descendants(xsi + "Body")
.Descendants(externalsystemreference + "grantapplication").Descendants(externalid + "grantapplication"))
{
string ss = itm.Element(d + "scheme").Value;
}
return true;
}
但是仍然没有为 ss 选择任何值,有人能看出这有什么问题吗
解决方案
如果我们假设您维护正确的 xml(根据上面的评论),并且您的方法确实收到了有效的“XmlDocument”;然后:
private static XmlDocument HisXml()
{
var xDoc = XDocument.Load("C:\\temp\\HisXml.xml");
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xDoc.ToString());
return xmlDocument;
}
然后,这是可行的,请注意,我会解决可能的 NULL 等问题,但您可以按照自己的方式处理 :) :(注意,如果您知道您只接收到,则无需通过“方案”节点进行循环一,你需要弄清楚
public static bool SaveContacts(XmlDocument application)
{
// COMMENTED CODE IS YOU OLD STUFF
//XDocument xmessage = XDocument.Parse(application.OuterXml);
//XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";//Envelop namespace s
//XNamespace xsd = "http://www.w3.org/2001/XMLSchema";//Envelop namespace s
//XNamespace soap = "http://schemas.xmlsoap.org/soap/envelope/";//Envelop namespace s
//XNamespace d = "http://tempuri.org/";//bookHotelResponse namespace
//XNamespace externalsystemreference = "0743A61C-B3F8-4B51-AF1E-FBE76172D34C";//d namespace
//XNamespace externalid = "d77ddae7-ad19-4c4a-b3bf-1e83df82e40f";//d namespace
XmlNodeList nodeList = application.GetElementsByTagName("scheme");
string hisStuff;
foreach (XmlNode n in nodeList)
{
hisStuff = n.InnerText;
}
//foreach (var itm in xmessage.Descendants(xsi + "Body")
// .Descendants(externalsystemreference + "grantapplication").Descendants(externalid + "grantapplication"))
//{
// string ss = itm.Element(d + "scheme").Value;
//}
return true;
}
推荐阅读
- c++ - STL 位集移位运算符导致分段错误
- c# - InvalidOperationException: Can't use schemaId .. 相同的 schemaId 已用于类型
- asp.net-mvc - 带有 AspNetCore.Authentication.OpenIdConnect 隐式流的片段 URL
- java - 在 libgdx 中部署到 web 时替换 isInstance() 方法
- netlogo - NetLogo:如何在乌龟出生后立即更换特定的标志?
- sql - 如何在我的 SQL 函数中使用 MAX ( COUNT (*))?
- c - 如何像在 python 中那样在 C 中乘以字符串?
- html - HTML 元关键字可以在单词之间包含空格吗?
- laravel - Laravel 验证 'starts_with' alpha
- javascript - 如何使用 JWT、护照身份验证 (NodeJS) 注销?