View Full Version : C# xml root element is missing

01-27-2014, 12:24 PM
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

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

XmlDocument xml = new XmlDocument();

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;


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

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

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

01-27-2014, 12:33 PM
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?

01-27-2014, 12:40 PM
<?xml version="1.0" encoding="utf-8"?>

01-27-2014, 12:43 PM
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?

01-27-2014, 12:48 PM
I'm trying to load the wow path on load and to save the wow path on folder browsing dialog

I want to use that xml file to save the wow path location and to use it when I need it

followed this tutorial http://www.codeexperts.com/showthread.php?452-C-XML-read-write-modify-delete-elements

01-27-2014, 12:56 PM
Ohh come on buddy there is much easier way ^_^ Let me show ya :P Will make screens :)

01-27-2014, 12:58 PM
I'm beginner into C#.. That would be great, thank you :3

01-27-2014, 01:20 PM
Okay first click new project:
Then select lang and name project:
Now double click on properties:
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:
Now lets enter code:

Here is 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;


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

01-27-2014, 01:24 PM
Im getting an error, if u have time and I dont disturb pls add my skype alexei.hd

01-27-2014, 05:12 PM
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:

_______ _____ _______ _______ _______
| ___| \| ___| | | | |
| ___| -- | ___| | | |
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;
XmlSerializer sr = new XmlSerializer((IClass.GetType()));
writer = new StreamWriter(filename);
sr.Serialize(writer, IClass);
if (writer != null)
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);
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.



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

textBoxWoWPath.Text = information.WoWPath;


WoWInformation information = new WoWInformation();

information.WoWPath = textBoxWoWPath.Text;
XmlSave.Save(information, "save.xml");

01-27-2014, 05:45 PM
Thanks Tommy :P