【问题标题】:Evaluating equations during "new EXlement"在“新 EXlement”期间评估方程
【发布时间】:2014-12-20 17:32:37
【问题描述】:

下面的 sn-p 不是对变量 (int)winTempplayerWIn 求和。我已经通过在调用createXML() 之前打印到屏幕来验证这两个变量都分配了正确的值。我的理论是你不能在创建新的 XELEMENT 时评估方程。任何人都可以验证这一点吗?

new XElement("playerWin", (int)winTemp + playerWin),

如果我在XElement 之外执行此操作,就像saveXML() 中的注释行一样,它会按预期工作。

  • 如果文件不存在 - 例外 XML 输出应为 Wins=10、Loss=1、Tie=0。
  • 如果文件存在 - 例外 XML 输出应为 Wins=20、Loss=2、Tie=0。

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;

namespace Testing_LINQ_to_XML
{
    class Program
    {
        static void Main()
        {
            Player p = new Player();
            p.readXML();
            p.toScreen();
            p.saveXML();
            p.readXML();
            p.toScreen();
            p.Exit();
        }
    }

    public class Player
    {
        public string path;
        public string playerName;
        public int playerWin = 10;
        public int playerLoss = 1;
        public int playerTie = 0;
        public int winTemp;
        public int lossTemp;
        public int tieTemp;

        public Player()
        {
            Console.WriteLine("Enter player Name...");
            playerName = Console.ReadLine();
            Console.WriteLine("n: " + playerName);
            getPath();
            Console.WriteLine("p: " + path);
            Console.ReadLine();
        }

        public string getPath()
        {
            path = (@"..\XML Saves\" + playerName + ".xml");
            return path;
        }

        public void toScreen()
        {
            Console.WriteLine("\nYour Record Is:\n");
            Console.WriteLine("Wins:     " + playerWin);
            Console.WriteLine("Losses:   " + playerLoss);
            Console.WriteLine("Ties:     " + playerTie);
        }

        public void saveXML()
        {
            if (File.Exists(path))
            {
                readXML();
                File.Delete(path);
                //playerWin = (int)winTemp;
                //playerLoss = (int)lossTemp;
                //playerTie = (int)tieTemp;
                createFile();
            }
            else
            {
                createFile();                
            }        
        }

        public void createFile()
        {
                XDeclaration _obj = new XDeclaration("1.0", "utf-8", "");
                XNamespace gameSaves = "gameSaves";
                XElement fileNew = new XElement("Root",
                                    new XElement("Player",
                                        new XElement("playerName", playerName),
                                        new XElement("Stats",
                                            new XElement("playerWin", (int)winTemp + playerWin),
                                            new XElement("playerLoss", (int)lossTemp + playerLoss),
                                            new XElement("playerTie", (int)tieTemp + playerTie))));


                fileNew.Save(path);
                Console.WriteLine("Save created: " + path);
        }

        public void readXML()
        {
            if (File.Exists(path))
            {
                var winTemp = new XElement("playerWin", playerWin);
                var lossTemp = new XElement("playerLoss", playerLoss);
                var tieTemp = new XElement("playerTie", playerTie);
            }
            else
            {
                Console.WriteLine("\nYou don't have any stats to show yet.  Get playing!!!");
            }

        }

        public void Exit()
        {
            Console.WriteLine("Press any key to exit.");
            Console.ReadLine();
        }
    }
}

XML 输出:

<Root>
   <Player>
      <playerName>Name</playerName>
      <Stats>
         <playerWin>10</playerWin>
         <playerLoss>1</playerLoss>
         <playerTie>0</playerTie>
      </Stats>
   </Player>
 </Root>

【问题讨论】:

  • 请添加更多代码,例如变量的定义以及现在生成的内容以及它与您的预期有何不同。现在很难说是哪里出了问题。
  • 当然还有 XML。基本上,我们需要能够重现问题才能帮助您。
  • 无法理解的问题 - 需要更集中的示例和有效的问题来帮助它。
  • 将此评论添加到 OP 改写问题。
  • 本地范围的 winTemp 变量隐藏了实例变量(在 readXML() 方法内)。因此 winTemp 实例变量根本没有设置并保持默认值,即 0。

标签: c# xml linq


【解决方案1】:

局部范围的winTemp 变量隐藏了实例变量(在readXML() 方法内)。因此,winTemp 实例变量根本没有设置,并在添加时保持默认值,即 0。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-30
    • 2016-05-01
    • 2018-04-15
    • 2013-03-30
    • 1970-01-01
    • 2017-09-17
    • 1970-01-01
    相关资源
    最近更新 更多