0

我有以下 SOAP 响应:

<?xml version="1.0"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header/>
<env:Body>  
<GetSalesResponse xmlns="http://api.dgm-uk.com/Publisher/schemas">
<status>ok</status>
-<sales>
     -<sale>
      <saleid>***</saleid>
      <campaignname>***</campaignname>
      <campaignid>***</campaignid>
      <advertisername>***</advertisername>
      </sale>
      -<sale>
      <saleid>***</saleid>
      <campaignname>***</campaignname>
      <campaignid>***</campaignid>
      <advertisername>***</advertisername>
      </sale> 
      ..............etc

现在,我能够使用以下代码获取所有销售元素:

XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlFile.NameTable);
nsmgr.AddNamespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
nsmgr.AddNamespace("GSR", "http://api.dgm-uk.com/Publisher/schemas");
var items = xmlFile.DocumentElement.SelectSingleNode("/soapenv:Envelope/soapenv:Body/GSR:GetSalesResponse/GSR:sales", nsmgr);

那么我如何在迭代销售元素时,选择它们的内在孩子,如 saleid 和 campaignid?我只是无法选择它们

4

1 回答 1

1

这有效...

XElement doc = XElement.Load("c:\\dd.xml");
XNamespace ns1 = "http://schemas.xmlsoap.org/soap/envelope/";
XNamespace ns2 = "http://api.dgm-uk.com/Publisher/schemas";

var items = doc.Descendants(ns1 + "Body").Descendants(ns2 + "GetSalesResponse").Descendants(ns2 + "sales").Descendants(ns2 + "sale")
.Select((x) => new
{
saleid = x.Element(ns2 + "saleid").Value,
campaignname = x.Element(ns2 + "campaignname").Value,
campaignid = x.Element(ns2 + "campaignid").Value,
advertisername = x.Element(ns2 + "advertisername").Value
});

foreach (var itm in items)
{
Console.WriteLine("saleid:" + itm.saleid);
Console.WriteLine("campaignid:" + itm.campaignid);
Console.WriteLine("campaignname:" + itm.campaignname);  
Console.WriteLine("advertisername:" + itm.advertisername);
}
于 2012-08-24T13:42:11.817 回答