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 に答える