简体   繁体   English

如何在C#中从此响应中提取并获取值

[英]How can I extract and get the values from this response in C#

Here is my code: 这是我的代码:

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);
    }

The output goes like this: 输出如下:

在此处输入图片说明

In the part of ExtData I got confused I do not know how can I extract those values like get the value of CardType, BatchNum, MID, TransID. 在ExtData中,我很困惑,我不知道如何提取那些值,例如获取CardType,BatchNum,MID,TransID的值。

How can I possibly extract those values? 如何提取这些值?

Any advice or suggestion how can I achieve this will be a big help. 任何建议或建议我将如何实现这将是一个很大的帮助。 Thanks! 谢谢!

base on the doc of api: 基于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>

This seems to be case of incorrectly formatted response. 这似乎是格式错误的响应的情况。 This is quite common with these kind of webservices. 对于这类Web服务,这是很常见的。 Don't expect it to get better any time soon. 不要指望它会很快好起来。

You simply need to brute-force the parsing and hope they don't change it or it is changed based on what kind of response you get. 您只需要蛮力分析,并希望他们不要改变它,否则它会根据您得到的响应而改变。

General algorithm would be: 通用算法为:

  1. Separate the 2 different parts 分开2个不同的部分
    • Coma-delimitered, equality-valued part 逗号分隔的等值部分
    • XML-formated part XML格式的部分
    • This could be done, maybe by finding first index of '<' and splitting the string into 2 in this place 可以做到这一点,方法是找到第一个索引“ <”,然后在该位置将字符串拆分为2
  2. Parse the 2 parts with different algorithm. 用不同的算法解析这两个部分。 In first case, the parsing shouldn't be a problem. 在第一种情况下,解析应该不是问题。 Just split along ',' and '=' and you get key-value pairs. 只需沿','和'='拆分,即可获得键值对。 In second case, you should use some XML parsing library to help you. 在第二种情况下,您应该使用一些XML解析库来帮助您。 Again, you get bunch of key-value pairs. 同样,您获得了一堆键值对。
  3. Put all key-value pairs into a string/string dictionary. 将所有键值对放入字符串/字符串字典中。

Using LINQ would be the easiest way, assuming the response is a string or something you can turn into a string: 使用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();

You would then have an anonymous type ( resp ) with the following propertries: 然后,您将获得具有以下属性的匿名类型( resp ):

resp.ExtData = "InvNum=1234567900,CardType=MasterCard,BatchNum=0011"
resp.BatchNum = "0011"
resp.MID = "4910354"
resp.TransID = "MCC1421250315"

You could then use normal String.Split operations on ExtData to get the data from that string you need. 然后,您可以对ExtData使用常规的String.Split操作来从所需的字符串中获取数据。

Not the prettiest solution, but sometimes you have to brute force methods. 不是最漂亮的解决方案,但有时您必须使用蛮力方法。 Adopt and modify to suit your needs/tastes. 采用并修改以适合您的需求/口味。

You need to extract them manually by using String.IndexOf..), and then String.SubString(..) etc 您需要使用String.IndexOf ..),然后再使用String.SubString(..)等手动提取它们

var cardType = data.Substring(data.IndexOf("<BatchNum>"), data.IndexOf("</BatchNum>") - data.IndexOf("<BatchNum>"));

With some exceptions handling code of course... 当然有一些例外情况处理代码...

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

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