Root Element is Missing error on well formed XML

Posted on 2008-10-05
Medium Priority
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 400 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The PowerShell Core 6.0 of .NET release is just the beginning. The upcoming PowerShell Core 6.1 would have artificial intelligence and internet of things capabilities. So many things to look forward to in the upcoming release.
The video provides a quick and easy steps to migrate MBOX file to well known Outlook PST and Office 365. Besides this, it also supports and migrates more than 20 email clients of MBOX which include AppleMail, Opera, Thunderbird and SeaMonkey effortl…
Watch the video to learn how one can deal with PST file corruption issue with an outstanding Kernel for Outlook PST Repair Tool easily. Using this tool, non-technical users can swiftly perform the repair process to restore their essential data witho…

621 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