?
Solved

Root Element is Missing error on well formed XML

Posted on 2008-10-05
5
Medium Priority
?
2,567 Views
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"?>
<GamePhrases>
	<Phrases>
		<phraseTitle>Place</phraseTitle>
		<phraseLine1>Washington DC</phraseLine1>
		<phraseLine2>a</phraseLine2>
		<phraseLine3>a</phraseLine3>
	</Phrases>
</GamePhrases>
 
// 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

0
Comment
Question by:kareemjg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 22

Expert Comment

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

Jim
0
 
LVL 26

Accepted Solution

by:
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
0
 

Author Comment

by:kareemjg
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();
            streamReader.Close();
            
            phraseDoc.LoadXml(text);

Open in new window

0
 

Author Closing Comment

by:kareemjg
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"
0
 

Author Comment

by:kareemjg
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"
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

752 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