Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用SOAP,如何在一个请求中传递多个ID?_C#_Xml_Post_Soap - Fatal编程技术网

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

C# 使用SOAP,如何在一个请求中传递多个ID?,c#,xml,post,soap,C#,Xml,Post,Soap,我有一个soap信封,我想在一个请求中传递200个ID。我已经创建了一个包含200个ID的列表,我正在将其传递给一个函数 private static XmlDocument CreateSoapEnvelope(List<UniqueIDs> ID) { string hexURL = "http://hex.com/test/getResponse"; string hexUID = "user"; string hexPW

我有一个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>
我怎么做

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

尝试使用foreach

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 x in ID)
        {
            xml+=@"<m:EndDevice>
            <m:mRID></m:mRID>
            <Names>
              <name>" + x.ToString() + @"</name>
            </Names>
            </m:EndDevice>";
        }


        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>";
我认为你的ToString是一种覆盖方法


希望能有帮助。

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>

您可以开始使用字符串生成器使其更加高效,并且可能更易于阅读,例如:私有静态字符串NameBlocksList id{var sb=new StringBuilder;ids.ForEachid=>sb.AppendNameBlockid;return sb.ToString;}然后在xml字符串中代替foreach,您可以只调用一次名称块,然后将单个结果与所有名称一起添加。