这是我的代码:

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/

10-14 18:28
查看更多