0

我在 XML 响应下面有这个。我需要将每个节点及其值存储在一个数组中,并将 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;元素 - 汇款人姓名;价值 - TEST SACCO;元素 - beneficiary_id;值 - 9;元素 - 受益人名称;价值 - KENYA USA DIASPORA SACCO LTD; 元素 - trans_type; 价值 - 账户;元素 - 目的地国家;价值 - 肯尼亚;元素 - source_currency; 价值 - 美元;元素 - source_transfer_amount;值 - 10.00;元素 - 速率;价值 - 83.4000;元素 - 目的地货币;价值——KES;元素-destination_amount;价值 - 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;元素 - 交付日期;价值 - 2012-12-07 00:00:00-05; 元素 - payment_token;值 - 3954d4d87aa2926dbb6150658881ec4622b101b6;

4

1 回答 1

0

使用您可以获得List的下一个代码KeyValuePair,其中键是标签名称,值是标签内容文本:

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 回答