需要关于使用XML作为数据库c#的建议

本文关键字:数据库 于使用 XML | 更新日期: 2023-09-27 18:16:01

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

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

需要关于使用XML作为数据库c#的建议

不重复。这是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>
​

代码

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

考虑面向对象,考虑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类来保存交易标记中的数据,然后使用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文件。它看起来会很相似。

另一方面,请阅读本文并考虑是否有更好的替代方案。