需要有关使用XML作为数据库C#的建议

时间:2015-10-05 15:48:01

标签: c# xml visual-studio

我正在创建一个存储用户输入数据的应用程序。

http://i827.photobucket.com/albums/zz200/ArnasG/question_in_stackowerflow__zps4f7uy3l7.png

由于缺乏经验,我遇到了一些问题,我希望将所有数据用户输入保存在XML文件中,并在程序下次启动时加载它。我有一个想法,使用数据集从XML文件中读取所有数据,然后使用该数据集的表[0](添加/删除行)。事实证明,我无法让它正常工作。它加载了我在之前尝试中创建的一些空白行和行,但实际上只有两行保存在XML文件中。我怎么能做这个工作?

感谢您的时间:)

实际XML文件:

http://i827.photobucket.com/albums/zz200/ArnasG/question_in_stackowerflow_V2_zpshmwjnllr.png

DataSet ListOfTrades = new DataSet();
DataTable Lentele = new DataTable();
ListOfTrades.Tables.Add(Lentele);


    // adding columns to the table

    try
    {

            DataColumn Pair = new DataColumn("Pair", typeof(string));
            Pair.AllowDBNull = false;
            DataColumn Entry = new DataColumn("Entry", typeof(string));
            Entry.AllowDBNull = false;
            DataColumn StopLoss = new DataColumn("StopLoss", typeof(string));
            StopLoss.AllowDBNull = false;
            DataColumn TakeProfit = new DataColumn("TakeProfit", typeof(string));
            TakeProfit.AllowDBNull = false;
            DataColumn TakeProfit1 = new DataColumn("TakeProfit1", typeof(string));
            TakeProfit1.AllowDBNull = false;
            DataColumn TakeProfit2 = new DataColumn("TakeProfit2", typeof(string));
            TakeProfit2.AllowDBNull = false;
            DataColumn TakeProfit3 = new DataColumn("TakeProfit3", typeof(string));
            TakeProfit3.AllowDBNull = false;
            DataColumn LongShort = new DataColumn("LongShort", typeof(string));
            LongShort.AllowDBNull = false;
            DataColumn WinLoss = new DataColumn("WinLoss", typeof(string));
            WinLoss.AllowDBNull = false;

            data.Tables[0].Columns.AddRange(new DataColumn[] {
        Pair, Entry, StopLoss, TakeProfit, TakeProfit1, TakeProfit2,
        TakeProfit3, LongShort, WinLoss
        });
        }

    catch(Exception Ex)
    {
        MessageBox.Show(Ex.Message);
    }

 // Adding new line to the table after user clicks save button

private void button1_Click(object sender, EventArgs e)
    {
        DataRow eilute = ListOfTrades.Tables[0].NewRow();
        eilute[0] = comboBox1.Text.ToString();
        eilute[1] = textBox1.Text.ToString();
        eilute[2] = textBox2.Text.ToString();
        eilute[3] = textBox3.Text.ToString();
        eilute[4] = textBox4.Text.ToString();
        eilute[5] = textBox5.Text.ToString();
        eilute[6] = textBox6.Text.ToString();
        if (radioButton1.Checked) { eilute[7] = "Long"; }
        else { eilute[7] = "short"; }
        if (radioButton1.Checked) { eilute[8] = "Win"; }
        else { eilute[8] = "Loss"; }

        ListOfTrades.Tables[0].Rows.Add(eilute);
        ListOfTrades.Tables[0].WriteXml(DefaultPathToJournalXML);

        dataGridView1.Update();
        dataGridView1.Refresh();

    }

2 个答案:

答案 0 :(得分:1)

没有重复。这是xml

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
   <Table1>
      <Pair>AUD/USD</Pair>
      <Entry>0.00000</Entry>
      <StopLoss>0.00000</StopLoss>
      <TakeProfit>0.00000</TakeProfit>
      <TakeProfit1>0.00000</TakeProfit1>
      <TakeProfit2>0.00000</TakeProfit2>
      <TakeProfit3>0.00000</TakeProfit3>
      <LongShort>short</LongShort>
      <WinLoss>loss</WinLoss>
   </Table1>
   <Table1>
      <Pair>AUD/USD</Pair>
      <Entry>0.00000</Entry>
      <StopLoss>0.00000</StopLoss>
      <TakeProfit>0.00000</TakeProfit>
      <TakeProfit1>0.00000</TakeProfit1>
      <TakeProfit2>0.00000</TakeProfit2>
      <TakeProfit3>0.00000</TakeProfit3>
      <LongShort>short</LongShort>
      <WinLoss>Loss</WinLoss>
   </Table1>
</NewDataSet>
​

这是代码

&#13;
&#13;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            DataSet ds = new DataSet();
            ds.ReadXml(FILENAME);
        }
    }
}
​
&#13;
&#13;
&#13;

答案 1 :(得分:0)

思考面向对象,并思考Linq.

例如,假设你有这个XML文件:

<trades>
    <trade>
        <pair>some pair</pair>
        <stop-loss>stop loss 1</stop-loss>
    </trade>
    <trade>
        <pair>other pair</pair>
        <stop-loss>stop loss 2</stop-loss>
    </trade>
</trades>

您可以创建一个Trade类来保存trade标记中的数据,然后使用Linq查询填充给定XML文件的对象:

class Trade
{
    public string Pair;
    public string StopLoss;
    // Other variables from trade tag would go here...

    // Function that can load trade objects from XML file into a list of Trade objects (List<Trade>)
    public static List<Trade> loadTrade(string xmlFilePath)
    {
        // Load your XML document given the path to the .xml file
        var doc = XDocument.Load(xmlFilePath);

        // For each trade element in the trades element
        var trades = (from trade in doc.Element("trades").Elements("trade")
                      select new Trade
                      {
                          // For each element in the trade element, put value in class variable
                          Pair = trade.Element("pair").Value,
                          StopLoss = trade.Element("stop-loss").Value
                      }).ToList<Trade>();

        return trades;
    }
}

当您准备保存到文件时,您基本上会执行与Linq查询相反的操作来创建XML文件。它看起来非常相似。

另一方面,请阅读this article并考虑是否有更好的选择。