这是我的代码:
private void button1_Click(object sender, EventArgs e)
{
var process = new com.globalpay.certapia.GlobalPayments();
var response = process.ProcessCreditCard("xxxxxx", "xxxxxx", "Sale",
textBox1.Text, textBox2.Text, "", "", textBox3.Text, "",
"", "", "", "", "");
MessageBox.Show("RespMSG: " + response.RespMSG
+ "\nMessage: " + response.Message
+ "\nAuthCode: " + response.AuthCode
+ "\nPNRef: " + response.PNRef
+ "\nHostCode: " + response.HostCode
+ "\nCVResultTXT: " + response.GetCVResultTXT
+ "\nCommercialCard: " + response.GetCommercialCard
+ "\nExtData: " + response.ExtData);
}
输出是这样的:
在 ExtData 部分,我感到困惑,我不知道如何提取这些值,例如获取 CardType、BatchNum、MID、TransID 的值。
我怎样才能提取这些值?
任何关于我如何实现这一点的建议或建议都会有很大的帮助。谢谢!
基于api文档:
<?xml version="1.0" encoding="utf-8" ?>
<Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="GlobalPayments">
<Result>0</Result>
<RespMSG>Approved</RespMSG>
<Message>AP</Message>
<AuthCode>000014</AuthCode>
<PNRef>564286</PNRef>
<HostCode>0032</HostCode>
<GetCVResultTXT>Service Not Requested</GetCVResultTXT>
<GetCommercialCard>False</GetCommercialCard>
<ExtData>InvNum=1234567900,CardType=MasterCard,BatchNum=0011<BatchNum>0011
</BatchNum><ReceiptData><MID>4910354</MID><Trans_Id>MCC1421250315
</Trans_Id></ReceiptData></ExtData>
</Response>
最佳答案
使用 LINQ 将是最简单的方法,假设响应是字符串或您可以将其转换为字符串的内容:
// XDocument.Parse will load a string into the XDocument object.
XDocument xDoc = XDocument.Parse(response);
// XNamespace is required in order to parse the document.
XNamespace ns = "GlobalPayments";
var resp = (from x in xDoc.Descendants(ns + "ExtData")
select new
{
ExtData = x.Value,
BatchNum = x.Element(ns + "BatchNum").Value,
MID = x.Element(ns + "MID").Value,
TransID = x.Element(ns + "TransID").Value
}).SingleOrDefault();
然后您将拥有一个具有以下属性的匿名类型 (resp
):
resp.ExtData = "InvNum=1234567900,CardType=MasterCard,BatchNum=0011"
resp.BatchNum = "0011"
resp.MID = "4910354"
resp.TransID = "MCC1421250315"
然后您可以在 ExtData 上使用普通的 String.Split 操作从您需要的字符串中获取数据。
这不是最漂亮的解决方案,但有时您必须使用蛮力方法。采用和修改以满足您的需求/品味。
关于c# - 如何从 C# 中的响应中提取和获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14301211/