简体   繁体   English

将XML节点存储在数组中

[英]Store XML nodes in array

I have this below XML response . 我在XML响应下面有这个。 I need to store each node and its value in a array and the attach the array in the URL as querystring and redirect to a diferrent page . 我需要将每个节点及其值存储在数组中,并将数组作为查询字符串附加到URL中,然后重定向到其他页面。 Please help 请帮忙

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

I have somehow reached to some code to get the output with some delimiter but still confused how to put the same in an array and paas it as querystring to next page 我以某种方式到达了一些代码,以使用一些定界符获取输出,但是仍然困惑如何将其放入数组并将其作为查询字符串停放在下一页

string str = ""; 字符串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;

output 输出

Element - response; 元素-回应; Element - responseId; 元素-responseId; Value - 76; 价值-76; Element - status; 元素-状态; Value - SUCCESS; 价值-成功; Element - result; 元素-结果; Element - reference_number; 元素-reference_number; Value - FA002900118; 价值-FA002900118; Element - remitter_id; 元素-remitter_id; Value - 10023; 价值-10023; Element - remitter_name; 元素-remitter_name; Value - TEST SACCO; 价值-测试SACCO; Element - beneficiary_id; 元素-beneficiary_id; Value - 9; 价值-9; Element - beneficiary_name; 元素-受益人名称; Value - KENYA USA DIASPORA SACCO LTD; 价值-肯尼亚美国DIASPORA SACCO LTD; Element - trans_type; 元素-trans_type; Value - Account; 价值-帐户; Element - destination_country; 元素-destination_country; Value - Kenya; 价值-肯尼亚; Element - source_currency; 元素-source_currency; Value - USD; 价值-美元; Element - source_transfer_amount; 元素-source_transfer_amount; Value - 10.00; 价值-10.00; Element - rate; 元素-费率; Value - 83.4000; 价值-83.4000; Element - destination_currency; 元素-destination_currency; Value - KES; 价值-KES; Element - destination_amount; 元素-destination_amount; Value - 834.00; 价值-834.00; Element - commission; 元素-佣金; Value - 5.00; 价值-5.00; Element - agent_fee; 元素-agent_fee; Value - 0.00; 价值-0.00; Element - hq_fee; 元素-hq_fee; Value - 0.00; 价值-0.00; Element - remitter_pay_amount; 元素-remitter_pay_amount; Value - 15.00; 价值-15.00; Element - agent_deduction; 元素-agent_deduction; Value - 2.50; 价值-2.50; Element - agent_to_pay_hq; 元素-agent_to_pay_hq; Value - 12.50; 价值-12.50; Element - delivery_date; 元素-delivery_date; Value - 2012-12-07 00:00:00-05; 价值-2012-12-07 00:00:00-05; Element - payment_token; 元素-Payment_token; Value - 3954d4d87aa2926dbb6150658881ec4622b101b6; 价值-3954d4d87aa2926dbb6150658881ec4622b101b6;

Use next code you can get List of KeyValuePair , where key is tag name and value is tag content text: 使用下面的代码,您可以获得KeyValuePair List ,其中key是标记名称,value是标记内容文本:

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

i think you can create query string form list. 我认为您可以创建查询字符串形式列表。 For redirect to other page in ASP.NET you can use HttpResponce.Redirect (more info http://msdn.microsoft.com/en-us/library/t9dwyts4.aspx ) 若要重定向到ASP.NET中的其他页面,可以使用HttpResponce.Redirect (更多信息http://msdn.microsoft.com/zh-cn/library/t9dwyts4.aspx

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

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