我创建了一个xml文件来存储用户事务历史记录。当用户想要显示交易历史记录时,我将它们添加到列表框中,但问题是元素的顺序不正确我希望最后一个交易在列表中显示。我已经尝试了很多方法对它们进行排序,但其中没有一个对我有用。我想如果我使用tcounter,我必须对元素进行排序,例如,如果在tcounter为4时添加了最后一项,我希望项目的顺序如下: 4 3 2 1 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 五 我添加交易的方法:
public void addtransaction(string accounttype, int accountnumber, string type, int ammount)
{
if (tcounter > 20)
{
tcounter = 0;
}
++tcounter;
XmlDocument doc = new XmlDocument();
doc.Load("USERS.xml");
{
if (id != 0)
{
foreach (XmlNode node in doc.DocumentElement)
{
if (id == int.Parse(node["ID"].InnerText))
{
node["tcounter"].InnerText = tcounter.ToString();
switch (tcounter)
{
case 1:
node["t1"].InnerText = tcounter+"."+accounttype + accountnumber + type + ammount + " " + time; doc.Save("USERS.xml");
break;
case 2:
node["t2"].InnerText = tcounter + "." + accounttype + accountnumber + type + ammount + " " + time; doc.Save("USERS.xml");
break;
case 3:
node["t3"].InnerText = tcounter + "." + accounttype + accountnumber + type + ammount + " " + time; doc.Save("USERS.xml");
break;
case 4:
node["t4"].InnerText = tcounter + "." + accounttype + accountnumber + type + ammount + " " + time; doc.Save("USERS.xml");
break;
case 5:
node["t5"].InnerText = tcounter + "." + accounttype + accountnumber + type + ammount + " " + time; doc.Save("USERS.xml");
break;
case 6:
node["t6"].InnerText = tcounter + "." + accounttype + accountnumber + type + ammount + " " + time; doc.Save("USERS.xml");
break;
case 7:
node["t7"].InnerText = tcounter + "." + accounttype + accountnumber + "." + type + ammount + " " + time; doc.Save("USERS.xml");
break;
case 8:
node["t8"].InnerText = tcounter + "." + accounttype + accountnumber + type + ammount + " " + time; doc.Save("USERS.xml");
break;
case 9:
node["t9"].InnerText = tcounter + "." + accounttype + accountnumber + type + ammount + " " + time; doc.Save("USERS.xml");
break;
case 10:
node["t10"].InnerText = tcounter + "." + accounttype + accountnumber + type + ammount + " " + time; doc.Save("USERS.xml");
break;
case 11:
node["t11"].InnerText = tcounter + "." + accounttype + accountnumber + type + ammount + " " + time; doc.Save("USERS.xml");
break;
case 12:
node["t12"].InnerText = tcounter + "." + accounttype + accountnumber + type + ammount + " " + time; doc.Save("USERS.xml");
break;
case 13:
node["t13"].InnerText = tcounter + "." + accounttype + accountnumber + type + ammount + " " + time; doc.Save("USERS.xml");
break;
case 14:
node["t14"].InnerText = tcounter + "." + accounttype + accountnumber + type + ammount + " " + time; doc.Save("USERS.xml");
break;
case 15:
node["t15"].InnerText = tcounter + "." + accounttype + accountnumber + type + ammount + " " + time; doc.Save("USERS.xml");
break;
case 16:
node["t16"].InnerText = tcounter + "." + accounttype + accountnumber + type + ammount + " " + time; doc.Save("USERS.xml");
break;
case 17:
node["t17"].InnerText = tcounter + "." + accounttype + accountnumber + type + ammount + " " + time; doc.Save("USERS.xml");
break;
case 18:
node["t18"].InnerText = tcounter + "." + accounttype + accountnumber + type + ammount + " " + time; doc.Save("USERS.xml");
break;
case 19:
node["t19"].InnerText = tcounter + "." + accounttype + accountnumber + type + ammount + " " + time; doc.Save("USERS.xml");
break;
case 20:
node["t20"].InnerText = tcounter + "." + accounttype + accountnumber + type + ammount + " " + time; doc.Save("USERS.xml");
break;
}
}
}
}
}
}
我的方法将元素从xml文件导入列表框:
private void accounthistorybttn_Click(object sender, EventArgs e)
{
XmlDocument doc11 = new XmlDocument();
doc11.Load("USERS.xml");
{
if (id != 0)
{
foreach (XmlNode node in doc11.DocumentElement)
{
if (id == int.Parse(node["ID"].InnerText))
{
item = node["t1"].InnerText;
item2 = node["t2"].InnerText;
item3 = node["t3"].InnerText;
item4 = node["t4"].InnerText;
item5 = node["t5"].InnerText;
item6 = node["t6"].InnerText;
item7 = node["t7"].InnerText;
item8 = node["t8"].InnerText;
item9 = node["t9"].InnerText;
item10 = node["t10"].InnerText;
item11 = node["t11"].InnerText;
item12 = node["t12"].InnerText;
item13 = node["t13"].InnerText;
item14 = node["t14"].InnerText;
item15 = node["t15"].InnerText;
item16 = node["t16"].InnerText;
item17 = node["t17"].InnerText;
item18 = node["t18"].InnerText;
item19 = node["t19"].InnerText;
item20 = node["t20"].InnerText;
List<string> ListaServizi = new List<string>() { item, item2, item3, item4, item5, item6, item7, item8, item9, item10, item11, item12, item13, item14, item15, item16, item17, item18, item19, item20 };
ListaServizi.Sort();
historylistBox1.Items.Clear();
historylistBox1.DataSource = ListaServizi;
我的XML文件代码:
<?xml version="1.0" encoding="utf-8" ?>
<root>
<User PIN ="5100">
<ID>123456</ID>
<tcounter>0</tcounter>
<Name>Kamal Al Nasr</Name>
<CheckingACCNum>52645852</CheckingACCNum>
<CheckingACCBalance>2500</CheckingACCBalance>
<SavingACCNum>741852</SavingACCNum>
<SavingACCBalance>2000</SavingACCBalance>
<Address>3500 John Merritt Blvd, Nashville, TN 37209</Address>
<Phone>(615) 963-5848</Phone>
<t1> </t1>
<t2> </t2>
<t3> </t3>
<t4> </t4>
<t5> </t5>
<t6> </t6>
<t7> </t7>
<t8> </t8>
<t9> </t9>
<t10> </t10>
<t11> </t11>
<t12> </t12>
<t13> </t13>
<t14> </t14>
<t15> </t15>
<t16> </t16>
<t17> </t17>
<t18> </t18>
<t19> </t19>
<t20> </t20>
</User>
<User PIN ="5110">
<tcounter>0</tcounter>
<ID>123457</ID>
<Name>Mike Stewart</Name>
<CheckingACCNum>8569745</CheckingACCNum>
<CheckingACCBalance>500</CheckingACCBalance>
<SavingACCNum>741850</SavingACCNum>
<SavingACCBalance>3000</SavingACCBalance>
<Address>110 Nevada Ave., Nashville, TN 37219</Address>
<Phone>(615) 963-5555</Phone>
<t1> </t1>
<t2> </t2>
<t3> </t3>
<t4> </t4>
<t5> </t5>
<t6> </t6>
<t7> </t7>
<t8> </t8>
<t9> </t9>
<t10> </t10>
<t11> </t11>
<t12> </t12>
<t13> </t13>
<t14> </t14>
<t15> </t15>
<t16> </t16>
<t17> </t17>
<t18> </t18>
<t19> </t19>
<t20> </t20>
</User>
<User PIN ="5111">
<ID>123450</ID>
<tcounter>0</tcounter>
<Name>Adam Lin</Name>
<CheckingACCNum>8565478</CheckingACCNum>
<CheckingACCBalance>4500</CheckingACCBalance>
<SavingACCNum>741844</SavingACCNum>
<SavingACCBalance>2000</SavingACCBalance>
<Address>1120 Virginia Ave., Nashville, TN 37210</Address>
<Phone>(615) 963-4444</Phone>
<t1> </t1>
<t2> </t2>
<t3> </t3>
<t4> </t4>
<t5> </t5>
<t6> </t6>
<t7> </t7>
<t8> </t8>
<t9> </t9>
<t10> </t10>
<t11> </t11>
<t12> </t12>
<t13> </t13>
<t14> </t14>
<t15> </t15>
<t16> </t16>
<t17> </t17>
<t18> </t18>
<t19> </t19>
<t20> </t20>
</User>
</root>