我想从SOAP消息中提取SOAP主体,我在SOAP主体中有一些数据,我必须在日期库中解析,所以这是代码:
public string Load_XML(string SoapMessage)
{
//check soap message
if (SoapMessage == null || SoapMessage.Length <= 0)
throw new Exception("Soap message not valid");
//declare some local variable
int iSoapBodyStartIndex = 0;
int iSoapBodyEndIndex = 0;
//load the Soap Message
//Učitaj string XML-a i pretvori ga u XML
XmlDocument doc = new XmlDocument();
try
{
doc.Load(SoapMessage);
}
catch (XmlException ex)
{
WriteErrors.WriteToLogFile("WS.LOAD_DOK_LoadXML", ex.ToString());
throw ex;
}
//search for the "http://schemas.xmlsoap.org/soap/envelope/" URI prefix
string prefix = string.Empty;
for (int i = 0; i < doc.ChildNodes.Count; i++)
{
System.Xml.XmlNode soapNode = doc.ChildNodes[i];
prefix = soapNode.GetPrefixOfNamespace("http://schemas.xmlsoap.org /soap/envelope/");
if (prefix != null && prefix.Length > 0)
break;
}
//prefix not founded.
if (prefix == null || prefix.Length <= 0)
throw new Exception("Can't found the soap envelope prefix");
//find soap body start index
int iSoapBodyElementStartFrom = SoapMessage.IndexOf("<" + prefix + ":Body");
int iSoapBodyElementStartEnd = SoapMessage.IndexOf(">", iSoapBodyElementStartFrom); -> HERE I HAVE AN ERROR!!!!
iSoapBodyStartIndex = iSoapBodyElementStartEnd + 1;
//find soap body end index
iSoapBodyEndIndex = SoapMessage.IndexOf("</" + prefix + ":Body>") - 1;
//get soap body (xml data)
return SoapMessage.Substring(iSoapBodyStartIndex, iSoapBodyEndIndex - iSoapBodyStartIndex + 1);
}
我在这里收到错误:
int iSoapBodyElementStartEnd = SoapMessage.IndexOf(">", iSoapBodyElementStartFrom);
错误:
指数超出范围。必须是非负数且小于 集合。
如果有人知道如何解决这个问题?
答案 0 :(得分:12)
对于这样的请求:
String request = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
xmlns:soapenc=""http://schemas.xmlsoap.org/soap/encoding/""
xmlns:xsd=""http://www.w3.org/2001/XMLSchema""
xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
<soap:Body>
<ResponseData xmlns=""urn:Custom"">some data</ResponseData>
</soap:Body>
</soap:Envelope>";
以下代码完成了解包数据并仅获取<ReponseData>
xml内容的工作:
XDocument xDoc = XDocument.Load(new StringReader(request));
var unwrappedResponse = xDoc.Descendants((XNamespace)"http://schemas.xmlsoap.org/soap/envelope/" + "Body")
.First()
.FirstNode
答案 1 :(得分:9)
Linq2Xml更易于使用。
string xml = @"<?xml version=""1.0"" encoding=""UTF-8"" ?>
<soap:envelope xmlns:xsd=""w3.org/2001/XMLSchema"" xmlns:xsi=""w3.org/2001/XMLSchema-instance"" xmlns:soap=""schemas.xmlsoap.org/soap/envelope/"">;
<soap:body>
<order> <id>1234</id> </order>
</soap:body>
</soap:envelope>";
XDocument xDoc = XDocument.Load(new StringReader(xml));
var id = xDoc.Descendants("id").First().Value;
<强> - 编辑 - 强>
循环body
中的元素:
XDocument xDoc = XDocument.Load(new StringReader(xml));
XNamespace soap = XNamespace.Get("schemas.xmlsoap.org/soap/envelope/");
var items = xDoc.Descendants(soap+"body").Elements();
foreach (var item in items)
{
Console.WriteLine(item.Name.LocalName);
}
答案 2 :(得分:2)
您可以使用app.controller('First', function ($scope, sharedProperties) {
sharedProperties.registerCallback(function(data) {
$scope.items = data;
});
});
来提取肥皂请求的正文。
GetElementsByTagName
答案 3 :(得分:0)
简单的解决方案,如果身体有多个孩子,而您只是想取出信封:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(placeYourXmlHere);
if (xmlDoc.DocumentElement.Name == "soapenv_Envelope")
{
string tempXmlString = xmlDoc.DocumentElement.InnerXml;
xmlDoc.LoadXml(tempXmlString);
}
如果要同时删除信封和正文,其中正文仅包含一个孩子:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(placeYourXmlHere);
while (xmlDoc.DocumentElement.Name == "soapenv_Envelope" || xmlDoc.DocumentElement.Name == "soapenv_Body")
{
string tempXmlString = xmlDoc.DocumentElement.InnerXml;
xmlDoc.LoadXml(tempXmlString);
}
现在xml缩小为正文的内容