C#-Soap如何从响应中获取值

我正在开发 C# 控制台应用程序。我硬编码了一个 soap 请求,我在其中传递了 200 个唯一 ID,如下所示。


要求


<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>"+userID+@"</UserID>

    </User>

    <MessageID>String</MessageID>

    <CorrelationID>String</CorrelationID>

    <Property>

      <Name>password</Name>

      <Value>"+pass+@"</Value>

    </Property>

    <Property>

      <Name>timeout(h)</Name>

      <Value>8</Value>

    </Property>

    </Header>

    <Payload>

    <m:MeterReadSchedule>

      <m:EndDevice>

        <m:mRID></m:mRID>

        <Names>

          <name>37010732575</name>

        </Names>

      </m:EndDevice>

       <m:EndDevice>

        <m:mRID></m:mRID>

        <Names>

          <name>37010719918</name>

        </Names>

      </m:EndDevice>       

        .

        .

        200 ID's        

        .

        .

       <m:EndDevice>

        <m:mRID></m:mRID>

        <Names>

          <name>37030315780</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>


通过它后,我能够得到带有值的响应


小唯快跑啊
浏览 79回答 1
1回答

ABOUTYOU

使用 xml linq。下面的代码从文件中读取并将结果放入字符串 xml 中。您的代码正在读取响应,应该将结果字符串转换为 xmlusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xml;using System.Xml.Linq;using System.IO;namespace ConsoleApplication110{&nbsp; &nbsp; class Program&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; const string FILENAME = @"c:\temp\test.xml";&nbsp; &nbsp; &nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string xml = File.ReadAllText(FILENAME);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XDocument doc = XDocument.Parse(xml);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XElement root = doc.Root;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XNamespace mNs = root.GetNamespaceOfPrefix("m");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<XElement> xMeterReadings = doc.Descendants(mNs + "MeterReading").ToList();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<MeterReadings> meterReadings = new List<MeterReadings>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (XElement xMeterReading in xMeterReadings)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MeterReadings newMeterReading = new MeterReadings();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; meterReadings.Add(newMeterReading);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XElement valuesInterval = xMeterReading.Element(mNs + "valuesInterval");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newMeterReading.start = (DateTime)valuesInterval.Element(mNs + "start");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newMeterReading.end = (DateTime)valuesInterval.Element(mNs + "end");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XElement meter = xMeterReading.Element(mNs + "Meter");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newMeterReading.name = (string)meter.Descendants(mNs + "name").FirstOrDefault();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newMeterReading.nameType = (string)meter.Descendants(mNs + "name").LastOrDefault();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newMeterReading.mRid = (string)meter.Element(mNs + "mRID");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<XElement> xReadings = xMeterReading.Elements(mNs + "Readings").ToList();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (XElement xReading in xReadings)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string readingType = (string)xReading.Element(mNs + "ReadingType").Attribute("ref");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ReadTypeEnum typeEnum = MeterReading.GetType(readingType);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (typeEnum != ReadTypeEnum.NONE)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (newMeterReading.meterReadings == null) newMeterReading.meterReadings = new List<MeterReading>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MeterReading meterReading = new MeterReading();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newMeterReading.meterReadings.Add(meterReading);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; meterReading.readingType = readingType;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; meterReading.start = (DateTime)valuesInterval.Descendants(mNs + "start").FirstOrDefault();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; meterReading.end = (DateTime)valuesInterval.Descendants(mNs + "end").FirstOrDefault();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; meterReading.value = (decimal)xReading.Element(mNs + "value");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; meterReading.timestamp = (DateTime)xReading.Element(mNs + "timeStamp");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public enum ReadTypeEnum&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; KWH1,&nbsp; &nbsp; &nbsp; &nbsp; KVH1,&nbsp; &nbsp; &nbsp; &nbsp; MDI1,&nbsp; &nbsp; &nbsp; &nbsp; KWH2,&nbsp; &nbsp; &nbsp; &nbsp; KVH2,&nbsp; &nbsp; &nbsp; &nbsp; MDI2,&nbsp; &nbsp; &nbsp; &nbsp; NONE&nbsp; &nbsp; }&nbsp; &nbsp; public class MeterReadings&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public DateTime start { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public DateTime end { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string name { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string nameType { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string mRid { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public List<MeterReading> meterReadings { get; set; }&nbsp; &nbsp; }&nbsp; &nbsp; public class MeterReading&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; private ReadTypeEnum _readingType { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string readingType&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string readTypeStr = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch (_readingType)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case ReadTypeEnum.KVH1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; readTypeStr = "13.26.0.1.1.1.12.0.0.0.0.1.0.0.224.3.72.0";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case ReadTypeEnum.KVH2:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; readTypeStr = "13.26.0.1.1.1.12.0.0.0.0.2.0.0.224.3.72.0";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case ReadTypeEnum.KWH1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; readTypeStr = "13.26.0.1.1.1.12.0.0.0.0.1.0.0.224.3.73.0";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case ReadTypeEnum.KWH2:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; readTypeStr = "13.26.0.1.1.1.12.0.0.0.0.2.0.0.224.3.72.0";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case ReadTypeEnum.MDI1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; readTypeStr = "13.8.0.6.1.1.8.0.0.0.0.1.0.0.224.3.38.0";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case ReadTypeEnum.MDI2:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; readTypeStr = "13.8.0.6.1.1.8.0.0.0.0.2.0.0.224.3.38.0";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return readTypeStr;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set { _readingType = GetType(value); }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public DateTime start { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public DateTime end { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public DateTime timestamp { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public decimal value { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public static ReadTypeEnum GetType(string value)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ReadTypeEnum readTypeEnum;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch (value)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "13.26.0.1.1.1.12.0.0.0.0.1.0.0.224.3.72.0":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; readTypeEnum = ReadTypeEnum.KWH1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "13.26.0.1.1.1.12.0.0.0.0.2.0.0.224.3.72.0":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; readTypeEnum = ReadTypeEnum.KVH2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "13.26.0.1.1.1.12.0.0.0.0.1.0.0.224.3.73.0":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; readTypeEnum = ReadTypeEnum.KVH1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "13.26.0.1.1.1.12.0.0.0.0.2.0.0.224.3.73.0":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; readTypeEnum = ReadTypeEnum.KVH2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "13.8.0.6.1.1.8.0.0.0.0.1.0.0.224.3.38.0":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; readTypeEnum = ReadTypeEnum.MDI1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "13.8.0.6.1.1.8.0.0.0.0.2.0.0.224.3.38.0":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; readTypeEnum = ReadTypeEnum.MDI2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; readTypeEnum = ReadTypeEnum.NONE;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return readTypeEnum;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP