2015-10-05 76 views
-1

我正在制作一个存储用户输入数据的应用程序。需要关于使用XML作为数据库的建议C#

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

    } 
+0

不要写空行成XML,那么你不必担心读空白。 – jdweng

回答

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> 

您可以创建一个贸易类,以保持在行业的标签的数据,然后使用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并考虑是否有更好的选择。

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