0

Visual Studio .Net 2010 で C# を使用して Windows アプリケーションを作成しています。API メソッドを使用して、インターネット上で動作する Web サービスがあります。以下はそのうちの 1 つの例です。これらは SOAP メッセージで動作します。したがって、この SOAP 要求でユーザー名とパスワードの文字列を入力します。

POST /anp.asmx HTTP/1.1
Host: api.anp.se
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://api.anp.se/GetOptOutList"

<?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>
    <GetOptOutList xmlns="http://api.anp.se/">
      <strUsername>string</strUsername>
      <strPassword>string</strPassword>
      <strMailingListID>string</strMailingListID>
    </GetOptOutList>
  </soap:Body>
</soap:Envelope>

SOAP 応答の形式は次のようになります。

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?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>
    <GetOptOutListResponse xmlns="http://api.anp.se/">
      <GetOptOutListResult>
        <xsd:schema>schema</xsd:schema>xml</GetOptOutListResult>
    </GetOptOutListResponse>
  </soap:Body>
</soap:Envelope>

呼び出しボタンをクリックすると、オンライン サービスは XML データセットを返します。結果のスニペットを次に示します。

<DataSet xmlns="http://api.anp.se/">
<xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="NewDataSet">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Report">
<xs:complexType>
<xs:sequence>
<xs:element name="SendQueueID" type="xs:int" minOccurs="0"/>
<xs:element name="SendTime" type="xs:dateTime" minOccurs="0"/>
<xs:element name="Subject" type="xs:string" minOccurs="0"/>
<xs:element name="SubscriberCount" type="xs:int" minOccurs="0"/>
<xs:element name="MailingList" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<NewDataSet xmlns="">
<Report diffgr:id="Report1" msdata:rowOrder="0">
<SendQueueID>1235163</SendQueueID>
<SendTime>2011-06-20T12:26:54.25+02:00</SendTime>
<Subject>Tack för din bokning</Subject>
<SubscriberCount>939</SubscriberCount>
<MailingList>Tack för din bokning 2011-06-20</MailingList>
</Report>
<Report diffgr:id="Report2" msdata:rowOrder="1">
<SendQueueID>1235146</SendQueueID>
<SendTime>2011-06-20T12:15:55.62+02:00</SendTime>
<Subject>Välkommen hem</Subject>
<SubscriberCount>688</SubscriberCount>
<MailingList>Välkommen hem 2011-06-20</MailingList>
</Report>
<Report diffgr:id="Report3" msdata:rowOrder="2">
<SendQueueID>1235128</SendQueueID>
<SendTime>2011-06-20T12:08:54.277+02:00</SendTime>
<Subject>Trevlig resa</Subject>
<SubscriberCount>832</SubscriberCount>
<MailingList>Trevlig resa 2011-06-20</MailingList>
</Report>
<Report diffgr:id="Report4" msdata:rowOrder="3">
<SendQueueID>1232232</SendQueueID>
<SendTime>2011-06-17T12:07:54.767+02:00</SendTime>
<Subject>Tack för din bokning</Subject>
<SubscriberCount>398</SubscriberCount>
<MailingList>Tack för din bokning 2011-06-17</MailingList>
</Report>

私にチュートリアルを紹介する場合は、それが要点を理解し、役に立つものであることを確認してください。データセットを操作する必要があります。データセット オブジェクトを取得する方法と、その中のデータにアクセスする方法を教えてください。自分のプロジェクトにサービス設定を追加する必要がありますか? また、どのタイプの .Net プロジェクトにする必要がありますか? 前もって感謝します。

4

1 に答える 1

2

ほぼすべての種類の C# プロジェクトが機能するはずです。プロジェクトを右クリックして、[サービス参照の追加] を選択するだけです。次に、URL を [アドレス] テキスト ボックスに入力し、名前を付けて、[OK] をクリックします。これにより、サービスによって返されるすべてのクラスを含む、必要なものすべてを含むサービス プロキシ クラスが作成されます。

私は長い間これをしていません。この非常に基本的な例では、Dilbert Web サービスを使用しています。

DilbertSoapClient client = new DilbertSoapClient();
string s = client.DailyDilbert(DateTime.Now);
DataTable table = new DataTable();
StringReader reader = new StringReader(s);
table.ReadXml(reader);
foreach (DataRow row in table.Rows)
{
    // do something with the row
}

もちろん、あなたが実際に使っているWebサービスを私は持っていないので、扱いは多少異なるかもしれませんが、これが基本的なモデルです。

于 2012-10-16T20:42:06.913 に答える