在C#

时间:2018-12-10 23:49:39

标签: c# asp.net .net web-services soap

如何从SOAP消息中解析特定部分并获取其值?

例如,如果肥皂响应消息是这样的:

      <?xml version="1.0" encoding="utf-8"?>
 <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
 xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
   <soap12:Body>
     <GetCountryListResponse xmlns="http://example.org/">
       <GetCountryListResult>
         <string>string</string>
         <string>string</string>
       </GetCountryListResult>
     </GetCountryListResponse>
   </soap12:Body>
 </soap12:Envelope>

我想获取GetCountryListResult值并将其保存到String变量中。

我在Java中找到了一个很好的答案:

Get Strings from SOAP Message in Java

2 个答案:

答案 0 :(得分:1)

此示例将从文件中读取xml并建立找到的字符串列表,但是您可以了解如何解析它。

using (FileStream fs = new FileStream(@"c:\temp\soap.xml", FileMode.Open))
{
    var sr = new StreamReader(fs);
    var str = sr.ReadToEnd();
    XmlDocument document = new XmlDocument();
    document.LoadXml(str);

    XmlNamespaceManager manager = new XmlNamespaceManager(document.NameTable);
    manager.AddNamespace("soap12", "http://www.w3.org/2003/05/soap-envelope");
    manager.AddNamespace("", "http://example.org/");
    XmlNodeList xnList = document.SelectNodes("//soap12:Envelope/soap12:Body/GetCountryListResponse/GetCountryListResult", manager);
    if (xnList.Count == 0) return;

    XmlNode countryListResult = xnList[0];
    List<string> result = new List<string>();
    foreach (XmlNode childNode in countryListResult.ChildNodes)
    {
        result.Add(childNode.FirstChild.Value);
    }
}

您还需要添加错误处理。

答案 1 :(得分:0)

var service = new WebService();     var result = service.Invoke(soapAction,方法,参数);

library(tmap)
library(grid)

data("World")

tm_shape(World) +
  tm_polygons("HPI", title = expression(italic(World - HPI))) + # set legend title to italic
  tm_layout(main.title = "HPI", # unfortunately, does not allow `expression`; try the `grid` hack
            main.title.position = "center") 

# Convert to gTree/list of grobs
g <- grid.grab()
View(g) # check the structure of the gTree; helps with identifying graphical elements
# Edit the fontface of the main title - from 1 (plain text) to 3 (italic); must be integer
g[["children"]][[1]][["children"]][["main_title"]][["children"]][[1]][["gp"]][["font"]] <- 3L
# Draw the edited gTree
grid.newpage(); grid.draw(g)