Root Element is Missing error on well formed XML

Posted on 2008-10-05
Last Modified: 2013-12-17
I am using C# to load a xml doucment to read.  When the software tries to read the XML is states root element is missing.  As far as I can tell the XML is well formed.  Not quite understanding where the problem is.
// XML Doucment

<?xml version="1.0"?>




		<phraseLine1>Washington DC</phraseLine1>





// Loading Class

 public class LoadPhrases


        public string phraseTitle;

        public string phraseLine1;

        public string phraseLine2;

        public string phraseLine3;

        public LoadPhrases getRandomPhrase()



            LoadPhrases thePhrase = new LoadPhrases();

            XmlDocument phraseDoc = new XmlDocument();

            phraseDoc.Load("GamePhrases.xml"); // <-- This throws the exception


            XmlNodeList title = phraseDoc.GetElementsByTagName("phraseTitle");

            XmlNodeList line1 = phraseDoc.GetElementsByTagName("phraseLine1");

            XmlNodeList line2 = phraseDoc.GetElementsByTagName("phraseLine2");

            XmlNodeList line3 = phraseDoc.GetElementsByTagName("phraseLine3");


            thePhrase.phraseTitle = title[0].InnerText;

            thePhrase.phraseLine1 = line1[0].InnerText;

            thePhrase.phraseLine2 = line2[0].InnerText;

            thePhrase.phraseLine3 = line3[0].InnerText;

            return thePhrase;




Open in new window

Question by:kareemjg
  • 3
LVL 22

Expert Comment

ID: 22644965
Can you attach the file? I see nothing wrong with the XML in the post.

LVL 26

Accepted Solution

Anurag Thakur earned 100 total points
ID: 22645020
tried u r code and its working perfectly
are you missing some details to tell us cause if you dont give us more details we wont be able to assist further

Author Comment

ID: 22645062
After some more troubleshooting it seems that the program is not seeing the file even though it is in the base directory.  In debuging the text variable returns a value of NULL.  Hrmm.. must be a problem with my paths.
 StreamReader streamReader = new StreamReader("GamePhrases.xml");

            string text = streamReader.ReadToEnd();




Open in new window


Author Closing Comment

ID: 31503184
Thanks for confirming what I wasnt doing wrong.  The problem was I didnt have the file set to copy to the content directory. "Copy if Newer"

Author Comment

ID: 22645091
Thanks for confirming what I wasnt doing wrong.  The problem was I didnt have the xml property file set to copy to the content directory. "Copy if Newer"

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Filename to be appended with DateTimeof Download 3 47
Entity Framework 7 32
How to add <appSettings> to the webconfig file 2 21
Reset login password 4 34
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now