0

为什么如果我保存我的变量并重新加载它们,那么 companyName 和 playerName 是这样的:System.Xml.XmlElement,而不是我写的?其他变量工作得很好。我为此苦苦挣扎了一段时间,所以我非常感谢任何帮助,谢谢。

public void LoadGamePrefs()

{

    string filepath = "c:/Users/gamePrefs.xml";
    XmlDocument xmlDoc = new XmlDocument();


    if(File.Exists (filepath))
    {
        xmlDoc.Load(filepath);
        XmlNodeList transformList = xmlDoc.GetElementsByTagName("GamePrefs");


        foreach (XmlNode transformInfo in transformList)
        {
            XmlNodeList transformcontent = transformInfo.ChildNodes;    

            foreach (XmlNode transformItems in transformcontent)
            {                  
                if(transformItems.Name == "firstStart")
                {
                    firstStart = bool.Parse(transformItems.InnerText);
                }
                if(transformItems.Name == "drawFirstGui")
                {
                    drawFirstStartGui = bool.Parse(transformItems.InnerText); 
                }
                if(transformItems.Name == "companyName")
                {
                    companyName = transformItems.InnerText; 
                }
                if(transformItems.Name == "playerName")
                {
                    playerName = transformItems.InnerText; 
                }
                if(transformItems.Name == "money")
                {
                    scriptMainBackground.money = int.Parse(transformItems.InnerText);
                }
                if(transformItems.Name == "year")
                {
                    year = int.Parse(transformItems.InnerText);
                }
                if(transformItems.Name == "month")
                {
                    month = int.Parse(transformItems.InnerText); 
                }
                if(transformItems.Name == "week")
                {
                    week = int.Parse(transformItems.InnerText);
                }
                if(transformItems.Name == "day")
                {
                    day = int.Parse(transformItems.InnerText); 
                }
            }
        }
    }
}



public void SaveGamePrefs()    {

    // Accesing other script and variable
    GameObject mainBackground = GameObject.FindGameObjectWithTag("MainBackground");

    ScriptMainBackground scriptMainBackground = mainBackground.GetComponent<ScriptMainBackground>();        

    string filepath = "c:/Users/gamePrefs.xml";
    XmlDocument xmlDoc = new XmlDocument();


    if(File.Exists (filepath))
    {
        xmlDoc.Load(filepath); 
        XmlElement elmRoot = xmlDoc.DocumentElement;    

        elmRoot.RemoveAll(); // remove all inside the transforms node.
        XmlElement elmNew = xmlDoc.CreateElement("GamePrefs");  

        XmlElement gamePrefs_firstStart = xmlDoc.CreateElement("firstStart"); 
        gamePrefs_firstStart.InnerText = firstStart.ToString();             

        XmlElement gamePrefs_drawFirstGui = xmlDoc.CreateElement("drawFirstGui");
        gamePrefs_drawFirstGui.InnerText = drawFirstStartGui.ToString();             

        XmlElement gamePrefs_companyName = xmlDoc.CreateElement("companyName");
        gamePrefs_companyName.InnerText = gamePrefs_companyName.ToString();            

        XmlElement gamePrefs_playerName = xmlDoc.CreateElement("playerName");
        gamePrefs_playerName.InnerText = gamePrefs_playerName.ToString();            

        XmlElement gamePrefs_Money = xmlDoc.CreateElement("money"); 
        gamePrefs_Money.InnerText = scriptMainBackground.money.ToString();        

        XmlElement gamePrefs_Year = xmlDoc.CreateElement("year"); 
        gamePrefs_Year.InnerText = week.ToString();            

        XmlElement gamePrefs_Month = xmlDoc.CreateElement("month"); 
        gamePrefs_Month.InnerText = week.ToString();             

        XmlElement gamePrefs_Week = xmlDoc.CreateElement("week"); 
        gamePrefs_Week.InnerText = week.ToString();    

        XmlElement gamePrefs_Day = xmlDoc.CreateElement("day"); 
        gamePrefs_Day.InnerText = day.ToString();             

        //XmlElement gamePrefs_GenreNumber = xmlDoc.CreateElement("genreNumber"); 
        //gamePrefs_Day.InnerText = genreNumber.ToString();


        elmNew.AppendChild(gamePrefs_firstStart);
        elmNew.AppendChild(gamePrefs_drawFirstGui);
        elmNew.AppendChild(gamePrefs_companyName);
        elmNew.AppendChild(gamePrefs_playerName);
        elmNew.AppendChild(gamePrefs_Money); 
        elmNew.AppendChild(gamePrefs_Week); 
        elmNew.AppendChild(gamePrefs_Day); 
        //elmNew.AppendChild(gamePrefs_GenreNumber);
        elmRoot.AppendChild(elmNew);     

        xmlDoc.Save(filepath); // save file.
    }
}
4

1 回答 1

1

问题在这里:

XmlElement gamePrefs_playerName = xmlDoc.CreateElement("playerName");
gamePrefs_playerName.InnerText = gamePrefs_playerName.ToString();

您正在通过调用对象将元素内部文本属性设置为XmlElement类 + 命名空间。.ToString()

您的意图尚不清楚,但我假设您需要将InnerText属性设置为playerName变量而不是XmlElement对象的字符串表示形式。

XmlElement gamePrefs_companyName = xmlDoc.CreateElement("companyName");
gamePrefs_companyName.InnerText = companyName; 

XmlElement gamePrefs_playerName = xmlDoc.CreateElement("playerName");
gamePrefs_playerName.InnerText = playerName;

请注意,ToString();这里的调用是不必要的,因为playerName/companyName从代码中判断变量已经看起来是字符串LoadGamePrefs()

于 2013-08-19T16:21:29.113 回答