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

C# 需要关于使用XML作为数据库C的建议吗,c#,xml,visual-studio,C#,Xml,Visual Studio,我正在制作一个应用程序来存储用户输入的数据 由于缺乏经验,我遇到了一点问题,我想将所有数据用户输入保存在XML文件中,并在下次程序启动时加载它。我想到了使用dataset从XML文件中读取所有数据,然后使用datasetadd/delete行的表[0]。结果我不能使它正常工作。它加载了一些空行和我在以前的尝试中创建的行,但实际上只有两行保存在XML文件中。我怎样才能做到这一点 感谢您抽出时间: 实际XML文件: <trades> <trade> &

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

由于缺乏经验,我遇到了一点问题,我想将所有数据用户输入保存在XML文件中,并在下次程序启动时加载它。我想到了使用dataset从XML文件中读取所有数据,然后使用datasetadd/delete行的表[0]。结果我不能使它正常工作。它加载了一些空行和我在以前的尝试中创建的行,但实际上只有两行保存在XML文件中。我怎样才能做到这一点

感谢您抽出时间:

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

思考面向对象,然后思考

例如,假设您有以下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>
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文件的对象:

<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>
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文件。它看起来非常相似


另一方面,阅读并考虑是否有更好的选择。 没有被复制。这是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>
​

不要将空行写入XML,这样就不必担心读取空行了。