0

XML response の下にこれがあります。各ノードとその値を配列に格納し、その配列をクエリ文字列として URL に添付して、別のページにリダイレクトする必要があります。助けてください

<responseId>76</responseId>
<status>SUCCESS</status>
<result>
    <reference_number>FA002900118</reference_number>
    <remitter_id>10023</remitter_id>
    <remitter_name>TEST SACCO</remitter_name>
    <beneficiary_id>9</beneficiary_id>
    <beneficiary_name>KENYA USA DIASPORA SACCO LTD</beneficiary_name>
    <trans_type>Account</trans_type>
    <destination_country>Kenya</destination_country>
    <source_currency>USD</source_currency>
    <source_transfer_amount>10.00</source_transfer_amount>
    <rate>83.4000</rate>
    <destination_currency>KES</destination_currency>
    <destination_amount>834.00</destination_amount>
    <commission>5.00</commission>
    <agent_fee>0.00</agent_fee>
    <hq_fee>0.00</hq_fee>
    <remitter_pay_amount>15.00</remitter_pay_amount>
    <agent_deduction>2.50</agent_deduction>
    <agent_to_pay_hq>12.50</agent_to_pay_hq>
    <delivery_date>2012-12-07 00:00:00-05</delivery_date>
    <payment_token>3954d4d87aa2926dbb6150658881ec4622b101b6</payment_token>
</result>

何らかの区切り文字で出力を取得するために何らかのコードに到達しましたが、同じものを配列に入れ、クエリ文字列として次のページに渡す方法をまだ混乱させています

文字列 str = "";

    XmlTextReader reader = new XmlTextReader("D:/TempXml.Xml");
    while (reader.Read())
    {
        XmlNodeType nodeType = reader.NodeType;
        switch (nodeType)
        {
            case XmlNodeType.Element:
                str+= " Element - " + reader.Name + ";";
                if (reader.HasAttributes)
                {
                    for (int i = 0; i < reader.AttributeCount; i++)
                    {
                        reader.MoveToAttribute(i);
                        str+= "Attribute - " + reader.Name + reader.Value;
                    }
                }
                break;
            case XmlNodeType.Text:
                str += " Value - " + reader.Value + ";";
                break;
        }
    }
    Label1.Text = str;

出力

要素 - 応答; 要素 - responseId; 値 - 76; 要素 - ステータス; 値 - 成功; 要素 - 結果; 要素 - 参照番号; 値 - FA002900118; 要素 - remitter_id; 値 - 10023; 要素 - remitter_name; 値 - TEST SACCO; 要素 - 受益者 ID; 値 - 9; 要素 - 受取人名; 値 - ケニア USA DIASPORA SACCO LTD; 要素 - trans_type; 値 - アカウント。要素 - destination_country; 値 - ケニア; 要素 - source_currency; 値 - 米ドル; 要素 - source_transfer_mount; 値 - 10.00; 要素 - レート; 値 - 83.4000; 要素 - destination_currency; 値 - KES; 要素 - destination_mount; 値 - 834.00; 要素 - 手数料; 値 - 5.00; 要素 - agent_fee; 値 - 0.00; 要素 - hq_fee; 値 - 0.00; 要素 - remitter_pay_amount; 値 - 15.00; 要素 - agent_deduction; 値 - 2.50; 要素 - agent_to_pay_hq; 値 - 12.50; 要素 - delivery_date; 値 - 2012-12-07 00:00:00-05; 要素 - payment_token; 値 - 3954d4d87aa2926dbb6150658881ec4622b101b6;

4

1 に答える 1

0

から取得できる次のコードを使用します。ここListKeyValuePair、キーはタグ名、値はタグ コンテンツ テキストです。

string responce = @"
<responce>
<responseId>76</responseId>
<status>SUCCESS</status>
<result>
<reference_number>FA002900118</reference_number>
<remitter_id>10023</remitter_id>
<remitter_name>TEST SACCO</remitter_name>
<beneficiary_id>9</beneficiary_id>
<beneficiary_name>KENYA USA DIASPORA SACCO LTD</beneficiary_name>
<trans_type>Account</trans_type>
<destination_country>Kenya</destination_country>
<source_currency>USD</source_currency>
<source_transfer_amount>10.00</source_transfer_amount>
<rate>83.4000</rate>
<destination_currency>KES</destination_currency>
<destination_amount>834.00</destination_amount>
<commission>5.00</commission>
<agent_fee>0.00</agent_fee>
<hq_fee>0.00</hq_fee>
<remitter_pay_amount>15.00</remitter_pay_amount>
<agent_deduction>2.50</agent_deduction>
<agent_to_pay_hq>12.50</agent_to_pay_hq>
<delivery_date>2012-12-07 00:00:00-05</delivery_date>
<payment_token>3954d4d87aa2926dbb6150658881ec4622b101b6</payment_token>
</result>
</responce>";

        StringReader reader=new StringReader(responce);

        XElement root = XElement.Load(reader);

        XElement resultNode = (XElement)root.Nodes().Single(node => ((XElement)node).Name == "result");

        IList<KeyValuePair<string, string>> result = new List<KeyValuePair<string, string>>();

        foreach(XElement item in resultNode.Nodes())
        {
            KeyValuePair<string, string> resultItem = new KeyValuePair<string, string>(item.Name.ToString(), item.Value);
            result.Add(resultItem);
        }

クエリ文字列形式のリストを作成できると思います。ASP.NET の他のページへのリダイレクトに使用できますHttpResponce.Redirect(詳細 はhttp://msdn.microsoft.com/en-us/library/t9dwyts4.aspx ) 。

于 2012-12-08T15:47:29.027 に答える