用xml保存字符串

时间:2013-08-19 16:16:48

标签: c# xml string

为什么如果我保存变量然后加载它们,那么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.
    }
}

1 个答案:

答案 0 :(得分: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()

中的代码的字符串