如何在 C# 上解析 SOAP 响应?
How to parse SOAP response on C#?
我在字符串变量 responseText
中得到 SOAP 响应,我应该如何从这些参数中提取值:
<ns1:Name>TEST TEST</ns1:Name>
<ns1:balance>10000</ns1:balance>
<ns1:customerId>POINT1</ns1:customerId>
感谢您的回复。
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>
尝试以下操作:
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; }
}
}
我在字符串变量 responseText
中得到 SOAP 响应,我应该如何从这些参数中提取值:
<ns1:Name>TEST TEST</ns1:Name>
<ns1:balance>10000</ns1:balance>
<ns1:customerId>POINT1</ns1:customerId>
感谢您的回复。
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>
尝试以下操作:
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; }
}
}