【问题标题】:Need advice on using XML as database C#需要关于使用 XML 作为数据库 C# 的建议
【发布时间】:2015-10-05 15:48:00
【问题描述】:

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

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


【解决方案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>
​

这是代码

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

【讨论】:

    【解决方案2】:

    思考面向对象,思考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 文件。它看起来会非常相似。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-26
      • 1970-01-01
      • 1970-01-01
      • 2010-10-18
      • 1970-01-01
      相关资源
      最近更新 更多