• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2570
  • Last Modified:

Root Element is Missing error on well formed XML

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
kareemjg
Asked:
kareemjg
  • 3
1 Solution
 
JimBrandleyCommented:
Can you attach the file? I see nothing wrong with the XML in the post.

Jim
0
 
Anurag ThakurCommented:
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
 
kareemjgAuthor Commented:
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
 
kareemjgAuthor Commented:
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
 
kareemjgAuthor Commented:
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

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now