0

why if I save my variables and load them back then companyName and playerName is this: System.Xml.XmlElement, instead of what I write? Other variables works just fine. I am struggling with this for a while, so I would really appreciate any help, thanks.

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.
    }
}
3
  • Can you please format your code - remove vertical spacing to make it readable Commented Aug 19, 2013 at 16:18
  • Do you have xsd for this file ? Commented Aug 19, 2013 at 16:24
  • 1
    perhaps you should simply use the serialization api, or the settings or any dedicated library. There's too much code repetition. DRY Commented Aug 19, 2013 at 16:32

1 Answer 1

1

The problem is here:

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

You are setting the elements inner text property to the XmlElement class + namespace by invoking .ToString() on the object.

Your intention is not clear but i assume you need to set the InnerText property to the playerName variable and not the string representation of the XmlElement object.

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

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

Note that the ToString(); call is unesseccary here as the playerName/companyName variables already appear to be strings judging from the code in LoadGamePrefs()

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

1 Comment

No problem, if this answers your question please consider accepting it as the answer so that it may help others in future.

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.