简体   繁体   English

如何解析 C# 上的 SOAP 响应?

[英]How to parse SOAP response on C#?

I get SOAP response in a string variable responseText , how I should extract from it values from these parameters:我在字符串变量responseText中得到 SOAP 响应,我应该如何从这些参数中提取值:

<ns1:Name>TEST TEST</ns1:Name>
<ns1:balance>10000</ns1:balance>
<ns1:customerId>POINT1</ns1:customerId>

Thanks for your reply.感谢您的回复。

SOAP response is in a string variable: SOAP 响应在字符串变量中:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns18:getCardDataResponse xmlns:ns1="http://www.ppc.com/gate/general/" xmlns:ns18="http://www.ppc.com/gate/command/getCardData/">
         <ns18:cardData>
            <ns1:Name>TEST TEST</ns1:Name>
            <ns1:Status>VALID CARD</ns1:Status>
            <ns1:accounts>
               <ns1:accountData>
                  <ns1:balance>10000</ns1:balance>
                  <ns1:customerId>POINT1</ns1:customerId>
               </ns1:accountData>
            </ns1:accounts>
         </ns18:cardData>
      </ns18:getCardDataResponse>
   </soap:Body>
</soap:Envelope>

Try following:尝试以下操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(FILENAME);
            XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
            Envelope envelope = (Envelope)serializer.Deserialize(reader);
        }
    }
    [XmlRoot(Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Envelope
    {
        [XmlElement(Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public Body Body {get;set;}
    }
    public class Body
    {
        [XmlArray(ElementName = "getCardDataResponse", Namespace = "http://www.ppc.com/gate/command/getCardData/")]
        [XmlArrayItem(ElementName = "cardData", Namespace = "http://www.ppc.com/gate/command/getCardData/")]
        public List<CardData> CardData { get; set; }
    }
    public class CardData
    {
        [XmlElement(Namespace = "http://www.ppc.com/gate/general/")]
        public string Name { get; set; }
        [XmlElement(Namespace = "http://www.ppc.com/gate/general/")]
        public string Status { get; set; }

        [XmlArray(ElementName = "accounts", Namespace = "http://www.ppc.com/gate/general/")]
        [XmlArrayItem(ElementName = "accountData", Namespace = "http://www.ppc.com/gate/general/")]
        public List<AccountData> accounts { get; set; }
    }
    public class AccountData
    {
        public int balance { get; set; }
        public string customerId { get; set; }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM