Read XML File

Could someone please let me know what I'm missing. Possibly IIS permissions, etc...
I am trying to read from an xml file, but keep getting this error:

XmlException: The data at the root level is invalid. Line 1, position 1.]
   System.Xml.XmlTextReader.ParseRoot() +295
   System.Xml.XmlTextReader.Read() +127
   System.Xml.XmlValidatingReader.ReadWithCollectTextToken()
   System.Xml.XmlValidatingReader.Read()
   System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace) +80
   System.Xml.XmlDocument.Load(XmlReader reader) +72
   System.Xml.XmlDocument.LoadXml(String xml)

I am:
Programming in c#
Running IIS 5.1
Microsoft Visual Studio 2003
.net Framework 1.1




Here is my code:


XmlDocument doc = new XmlDocument();
string xmlString = "c:\\temp\\MenuItems.xml";
doc.LoadXml(xmlString);
XmlElement elm;
XmlNode root = doc.DocumentElement;
elm = (XmlElement)root.SelectSingleNode("/MENUITEM/OBJECT");
string title = elm.InnerText;



Here is my xml file in it's entirety:

<?xml version="1.0" ?>
<MENUBAR>
      <MENUITEM id="8">
            <OBJECT>Account</OBJECT>
            <NAVIGATEURL>~/Web/AccountSearch.aspx?obj=</NAVIGATEURL>
            <NUMBER>8</NUMBER>
      </MENUITEM>
</MENUBAR>
LVL 1
cdfllcAsked:
Who is Participating?
 
dunglaConnect With a Mentor Commented:
You have load XML and give the path of XML file, then your XMLDocument will be nothing. Change your code:

XmlDocument doc = new XmlDocument();
string xmlString = "c:\\temp\\MenuItems.xml";
doc.LoadXml(xmlString);

To:

XmlDocument doc = new XmlDocument();
string xmlString = "c:\\temp\\MenuItems.xml";
doc.Load(xmlString);
0
 
prasanna051400Commented:
The Description of Load & LoadXml is given as
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemxmlxmldocumentmemberstopic.asp
Load
Overloaded. Loads the specified XML data.
Note   The Load method always preserves significant white space. The PreserveWhitespace property determines whether or not white space is preserved. The default is false, white space is not preserved.

LoadXml
Supported by the .NET Compact Framework.
 Loads the XML document from the specified string.
So Why cant we use LoadXML to load the document and then retrieve , can you explain why LoadXml Bombs ?

Regards,
Prasanna Rambhatla
0
 
cdfllcAuthor Commented:
Dungla, thanks I am getting farther now, but when the code gets to:

elm = (XmlElement)root.SelectSingleNode("/MENUITEM/OBJECT");
string title = elm.InnerText;

The elm variable says "undefined value"

Am I not referring to the Object value correctly?

thanks!
cdfllc
0
 
cdfllcAuthor Commented:
Nevermind, I figured it out. I was referring to the node as /MENUITEM/OBJECT
instead of /MENUBAR/MENUITEM/OBJECT

That did it. I figured it was something easy, but Load/LoadXML ....

thanks,
cdfllc
0
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.

All Courses

From novice to tech pro — start learning today.