使用SOAP,如何在一个请求中传递多个ID?

时间:2019-04-22 18:40:07

标签: c# xml post soap

我有一个肥皂信封,我想在一个请求中传递200 ID。我已经创建了200个ID的列表,并将其传递给函数。

private static XmlDocument CreateSoapEnvelope(List<UniqueIDs> ID)
    {
        string hexURL = "http://hex.com/test/getResponse";
        string hexUID = "user";
        string hexPWD = "123456";
        string uniqueID = ID.ToString();

        string startTime = "2019-03-01T00:00:00+04:30";
        string endTime = "2019-03-31T00:00:00+04:30";
        string timestamp = DateTime.UtcNow.ToString("o");

        string xml = @"<soapenv:Envelope 
          xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" 
          xmlns:soap=""http://soap.inf.hexing.cn"">
          <soapenv:Header/>
          <soapenv:Body>
          <soap:doCommand>
         <!--Optional:-->
        <arg0><![CDATA[<?xml version=""1.0"" encoding=""UTF-8""?>
       <RequestMessage xmlns=""http://iec.ch/TC57/2011/schema/message""
       xmlns:m=""http://iec.ch/TC57/2011/MeterReadSchedule#"" 
       xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
       xsi:schemaLocation=""http://iec.ch/TC57/2011/schema/message Message.xsd"">
      <Header>
      <Verb>create</Verb>
      <Noun>MeterReadSchedule</Noun>
      <Revision>2.0</Revision>
      <Timestamp>" + timestamp + @"</Timestamp>
      <Source>Hesco</Source>
      <AsyncReplyFlag>false</AsyncReplyFlag>
      <ReplyAddress>" + hexURL + @"</ReplyAddress>
      <AckRequired>false</AckRequired>
      <User>
      <UserID>" +hexUID+ @"</UserID>
     </User>
     <MessageID>String</MessageID>
     <CorrelationID>String</CorrelationID>
     <Property>
     <Name>password</Name>
     <Value>"+hexPWD+@"</Value>
     </Property>
     <Property>
     <Name>timeout(h)</Name>
     <Value>8</Value>
     </Property>
     </Header>
     <Payload>
     <m:MeterReadSchedule>

    <m:EndDevice>
    <m:mRID></m:mRID>
    <Names>
      <name>" + uniqueID + @"</name>
    </Names>
    </m:EndDevice>

    <m:ReadingType>
    <m:Names>
      <m:name>MonthlyBilling</m:name>
      <m:NameType>
        <m:name>BillingType</m:name>
      </m:NameType>
    </m:Names>
    </m:ReadingType>
    <m:TimeSchedule>
    <m:recurrencePeriod>86400</m:recurrencePeriod>
    <m:scheduleInterval>
      <m:end>" + endTime + @"</m:end>
      <m:start>" + startTime + @"</m:start>
    </m:scheduleInterval>
    </m:TimeSchedule>
    </m:MeterReadSchedule>
    </Payload>
    </RequestMessage>]]></arg0>
    </soap:doCommand>
    </soapenv:Body>
    </soapenv:Envelope>";


        XmlDocument soapEnvelopeDocument = new XmlDocument();
        soapEnvelopeDocument.LoadXml(xml);
        return soapEnvelopeDocument;
    }

在上面的代码中,我将ID列表传递给函数。在函数中,我添加了XML作为字符串。在此XML请求中,我想使用正确的标记(如

之下)传递这些ID。
<m:EndDevice>
<m:mRID></m:mRID>
<Names>
  <name>" + uniqueID[1] + @"</name>//
</Names>
</m:EndDevice>
<m:EndDevice>
<m:mRID></m:mRID>
<Names>
  <name>" + uniqueID[2] + @"</name>//
</Names>
</m:EndDevice>
.
.
.
.
.
<m:EndDevice>
<m:mRID></m:mRID>
<Names>
  <name>" + uniqueID[200] + @"</name>//
</Names>
</m:EndDevice>

我该怎么办?

任何帮助都会得到高度重视。

2 个答案:

答案 0 :(得分:3)

尝试使用foreach。

-u

我认为您的ToString是重写方法。

希望有帮助。

答案 1 :(得分:2)

ID.ToString()不会按照您的想法做。这将返回参数类型的类型名称。您需要展开列表。我不知道什么是UniqueId类型,但在那里有一个使用Guid的示例:

        private static string NameBlock(Guid uniqueID)
    {
        return @"
<m:EndDevice>
<m:mRID></m:mRID>
<Names>
  <name>" + uniqueID + @"</name>
</Names>
</m:EndDevice>";
    }

    private static XmlDocument CreateSoapEnvelope(List<Guid> ID)
    {
        string hexURL = "http://hex.com/test/getResponse";
        string hexUID = "user";
        string hexPWD = "123456";

        string startTime = "2019-03-01T00:00:00+04:30";
        string endTime = "2019-03-31T00:00:00+04:30";
        string timestamp = DateTime.UtcNow.ToString("o");

        var nameBlocks = ID.Select(NameBlock).ToArray();

        string xml = @"<soapenv:Envelope 
      xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" 
      xmlns:soap=""http://soap.inf.hexing.cn"">
      <soapenv:Header/>
      <soapenv:Body>
      <soap:doCommand>
     <!--Optional:-->
    <arg0><![CDATA[<?xml version=""1.0"" encoding=""UTF-8""?>
   <RequestMessage xmlns=""http://iec.ch/TC57/2011/schema/message""
   xmlns:m=""http://iec.ch/TC57/2011/MeterReadSchedule#"" 
   xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
   xsi:schemaLocation=""http://iec.ch/TC57/2011/schema/message Message.xsd"">
  <Header>
  <Verb>create</Verb>
  <Noun>MeterReadSchedule</Noun>
  <Revision>2.0</Revision>
  <Timestamp>" + timestamp + @"</Timestamp>
  <Source>Hesco</Source>
  <AsyncReplyFlag>false</AsyncReplyFlag>
  <ReplyAddress>" + hexURL + @"</ReplyAddress>
  <AckRequired>false</AckRequired>
  <User>
  <UserID>" + hexUID + @"</UserID>
 </User>
 <MessageID>String</MessageID>
 <CorrelationID>String</CorrelationID>
 <Property>
 <Name>password</Name>
 <Value>" + hexPWD + @"</Value>
 </Property>
 <Property>
 <Name>timeout(h)</Name>
 <Value>8</Value>
 </Property>
 </Header>
 <Payload>
 <m:MeterReadSchedule>";


        foreach(var nameBlock in nameBlocks)
        {
            xml += nameBlock;
        }


        xml += @"
< m:ReadingType>
<m:Names>
  <m:name>MonthlyBilling</m:name>
  <m:NameType>
    <m:name>BillingType</m:name>
  </m:NameType>
</m:Names>
</m:ReadingType>
<m:TimeSchedule>
<m:recurrencePeriod>86400</m:recurrencePeriod>
<m:scheduleInterval>
  <m:end>" + endTime + @"</m:end>
  <m:start>" + startTime + @"</m:start>
</m:scheduleInterval>
</m:TimeSchedule>
</m:MeterReadSchedule>
</Payload>
</RequestMessage>]]></arg0>
</soap:doCommand>
</soapenv:Body>
</soapenv:Envelope>";

然后将xml加载到文档中。不是很优雅,但是会返回:

<soapenv:Envelope 
      xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
      xmlns:soap="http://soap.inf.hexing.cn">
      <soapenv:Header/>
      <soapenv:Body>
      <soap:doCommand>
     <!--Optional:-->
    <arg0><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
   <RequestMessage xmlns="http://iec.ch/TC57/2011/schema/message"
   xmlns:m="http://iec.ch/TC57/2011/MeterReadSchedule#" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://iec.ch/TC57/2011/schema/message Message.xsd">
  <Header>
  <Verb>create</Verb>
  <Noun>MeterReadSchedule</Noun>
  <Revision>2.0</Revision>
  <Timestamp>2019-04-22T19:04:29.4296720Z</Timestamp>
  <Source>Hesco</Source>
  <AsyncReplyFlag>false</AsyncReplyFlag>
  <ReplyAddress>http://hex.com/test/getResponse</ReplyAddress>
  <AckRequired>false</AckRequired>
  <User>
  <UserID>user</UserID>
 </User>
 <MessageID>String</MessageID>
 <CorrelationID>String</CorrelationID>
 <Property>
 <Name>password</Name>
 <Value>123456</Value>
 </Property>
 <Property>
 <Name>timeout(h)</Name>
 <Value>8</Value>
 </Property>
 </Header>
 <Payload>
 <m:MeterReadSchedule>
<m:EndDevice>
<m:mRID></m:mRID>
<Names>
  <name>5e560c7e-c3b5-44aa-92eb-f1d2bb508b91</name>
</Names>
</m:EndDevice>
<m:EndDevice>
<m:mRID></m:mRID>
<Names>
  <name>a68c265b-5d35-4870-8d9c-9941653e5ab4</name>
</Names>
</m:EndDevice>
<m:EndDevice>
<m:mRID></m:mRID>
<Names>
  <name>a1894cb2-b538-4e35-869c-d580abcd1862</name>
</Names>
</m:EndDevice>
< m:ReadingType>
<m:Names>
  <m:name>MonthlyBilling</m:name>
  <m:NameType>
    <m:name>BillingType</m:name>
  </m:NameType>
</m:Names>
</m:ReadingType>
<m:TimeSchedule>
<m:recurrencePeriod>86400</m:recurrencePeriod>
<m:scheduleInterval>
  <m:end>2019-03-31T00:00:00+04:30</m:end>
  <m:start>2019-03-01T00:00:00+04:30</m:start>
</m:scheduleInterval>
</m:TimeSchedule>
</m:MeterReadSchedule>
</Payload>
</RequestMessage>]]></arg0>
</soap:doCommand>
</soapenv:Body>
</soapenv:Envelope>
相关问题