将XML节点存储在数组中

时间:2012-12-08 15:02:42

标签: c# .net

我有以下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>

我已经以某种方式达到某些代码以获得带有分隔符的输出但仍然混淆了如何将相同的数据放入数组并将其作为查询字符串传递给下一页

string 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;元素 - 状态;价值 - 成功;元素 - 结果;元素 - reference_number;价值 - FA002900118;元素 - remitter_id;价值 - 10023;元素 - remitter_name;价值 - TEST SACCO;元素 - beneficiary_id;价值 - 9;元素 - beneficiary_name;价值 - KENYA USA DIASPORA SACCO LTD;元素 - trans_type;价值 - 账户;元素 - destination_country;价值 - 肯尼亚;元素 - source_currency;价值 - 美元;元素 - source_transfer_amount;价值 - 10.00;元素率;价值 - 83.4000;元素 - destination_currency;价值 - 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;元素 - delivery_date;价值 - 2012-12-07 00:00:00-05;元素 - payment_token;价值 - 3954d4d87aa2926dbb6150658881ec4622b101b6;

1 个答案:

答案 0 :(得分:0)

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

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