-1

I am making an application that stores data that user inputs.

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

I have a bit of an issue because of my lack of experience, I want to save all the data user inputs in XML file and to load it when program starts next time. I had an idea to use dataset to read all the data from XML file and then work with the table[0] of that dataset(add/delete rows). It turn out that I can not make it to work properly. It loads some blank lines and lines that I created in previous tries, but there is only two lines that are actually saved in XML file. How could I make this work?

Thank you for your time :)

Actual XML file:

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

    }
1
  • Don't write blank lines into XML, then you don't have to worry about reading the blanks. Commented Oct 5, 2015 at 17:46

2 Answers 2

2

Not getting duplicated. here is the 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>
​

Here is code

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

Sign up to request clarification or add additional context in comments.

Comments

0

Think object oriented, and think Linq.

For example, let's say you have this XML file:

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

You can create a Trade class to hold the data in the trade tags and then use a Linq query to populate the object given the XML file:

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

When you're ready to save to a file you basically do the opposite of the Linq query to create an XML file. It will look very similar.

On there other hand, read this article and consider if there's a better alternative.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.