?
Solved

Read XML File

Posted on 2005-04-11
4
Medium Priority
?
280 Views
Last Modified: 2008-02-01
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>
0
Comment
Question by:cdfllc
  • 2
4 Comments
 
LVL 13

Accepted Solution

by:
dungla earned 2000 total points
ID: 13758563
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
 

Expert Comment

by:prasanna051400
ID: 13759661
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
 
LVL 1

Author Comment

by:cdfllc
ID: 13764458
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
 
LVL 1

Author Comment

by:cdfllc
ID: 13764544
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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Loops Section Overview
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month14 days, 2 hours left to enroll

807 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