• This is a read only backup of the old Emudevs forum. If you want to have anything removed, please message me on Discord: KittyKaev

C# xml root element is missing

AlexeWarr

Epic Member
Hello, I'm trying to write to an xml file to save a folder path location but Im getting an error "root element is missing".

I have marked where the error is from

Code:
        private void wowpathbutton_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                wowpath.Text = folderBrowserDialog1.SelectedPath;

                XmlDocument xml = new XmlDocument();
                [B][U][COLOR="#FF0000"]xml.Load("save.xml");[/COLOR][/U][/B]

                if (xml.ChildNodes.Count == 0) // if xml file is empty, write elements and save path
                {
                    XmlElement newcatalogentry = xml.CreateElement("settings");

                    XmlElement firstelement = xml.CreateElement("WoWPath");

                    firstelement.InnerText = folderBrowserDialog1.SelectedPath;

                    newcatalogentry.AppendChild(firstelement);

                    xml.DocumentElement.InsertAfter(newcatalogentry, xml.DocumentElement.LastChild);

                    xml.Save("save.xml");
                }
                else // we save the wow path
                {
                    XmlNodeList reminders = xml.SelectNodes("//settings");

                    foreach (XmlNode reminder in reminders)
                    {
                        reminder.SelectSingleNode("WoWPath").InnerText = folderBrowserDialog1.SelectedPath;
                    }
                }
            }
        }
 

uDev

Illustrious Member
Can you post your xml file also please?

- - - Updated - - -

Also you do realize that same thing you are trying to make there is already configuration for it? using Settings in properties?
 

uDev

Illustrious Member
Code:
use  XmlNodeList nodes = doc.GetElementsByTagName("WoWPath");

But what are you trying to do actually? Saving wow path to file and then launcher(I guess?) or what ever it is to load path on start?
 

uDev

Illustrious Member
Ohh come on buddy there is much easier way ^_^ Let me show ya :p Will make screens :)
 

uDev

Illustrious Member
Okay first click new project:
http://screencast.com/t/zgmJJHSo
Then select lang and name project:
http://screencast.com/t/MMdQB7Flf1
Now double click on properties:
http://screencast.com/t/8fiI7LyysQoV
Now go to settings and make WowLocation Name type is string scope is user(if there are more users on pc they will need to choose own folder)
Now go back to designer:
http://screencast.com/t/EkQDx08b - And double left click on our form.
You will get to source code of it with this function:
http://screencast.com/t/B8twjuq1
Now lets enter code:
http://screencast.com/t/VOvef8ix

Here is code:
Code:
 if (string.IsNullOrEmpty(Settings.Default.WowLocation) || !Directory.Exists(Settings.Default.WowLocation))
            {
                using (var fbd = new FolderBrowserDialog())
                {
                    if (!fbd.ShowDialog().ToString().Equals("OK")) return;
                    Settings.Default.WowLocation = fbd.SelectedPath;
                    Settings.Default.Save();
                }

            }

If you are stuck somewhere send me your skype name :)
 

Tommy

Founder
Not sure why you want to go through that trouble when it can be very simple. I created an xml load and save system that I use for all of my projects:

Code:
/*
          _______ _____  _______ _______ _______ 
         |    ___|     \|    ___|   |   |   |   |
         |    ___|  --  |    ___|       |   |   |
         |_______|_____/|_______|__|_|__|_______| 
     Copyright (C) 2014 EmuDevs <http://www.emudevs.com/>
 
  This program is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by the
  Free Software Foundation; either version 2 of the License, or (at your
  option) any later version.
 
  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  more details.
 
  You should have received a copy of the GNU General Public License along
  with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.IO;
using System.Xml.Serialization;

namespace Your_NAMESPACE
{
    public class XmlSave
    {
        public static void Save(object IClass, string filename)
        {
            StreamWriter writer = null;
            try
            {
                XmlSerializer sr = new XmlSerializer((IClass.GetType()));
                writer = new StreamWriter(filename);
                sr.Serialize(writer, IClass);
            }
            finally
            {
                if (writer != null)
                    writer.Close();
                writer = null;
            }
        }
    }

    public class XmlLoad<T>
    {
        public static Type type;

        public XmlLoad()
        {
            type = typeof(T);
        }

        public T Load(string fileName)
        {
            T result;
            XmlSerializer xmlSerial = new XmlSerializer(type);
            FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
            result = (T)xmlSerial.Deserialize(fs);
            fs.Close();
            return result;
        }
    }

    public class WoWInformation
    {
        public string WoWPath { get; set; }
        // Settings - Not sure what this is, string, integer?
    }
}

'public class WoWInformation' is the class you'll use to store the data.

Usage:

Loading:

Code:
                WoWInformation information = new WoWInformation();
                XmlLoad<WoWInformation> informationLoad = new XmlLoad<WoWInformation>();
                information = informationLoad.Load("save.xml");

                textBoxWoWPath.Text = information.WoWPath;

Saving:

Code:
                WoWInformation information = new WoWInformation();

                information.WoWPath = textBoxWoWPath.Text;
                XmlSave.Save(information, "save.xml");
 
Last edited:
Top