Read XML File

Posted on 2005-04-11
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.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";
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" ?>
      <MENUITEM id="8">
Question by:cdfllc
    LVL 13

    Accepted Solution

    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";


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

    Expert Comment

    The Description of Load & LoadXml is given as
    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.

    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 ?

    Prasanna Rambhatla
    LVL 1

    Author Comment

    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?

    LVL 1

    Author Comment

    Nevermind, I figured it out. I was referring to the node as /MENUITEM/OBJECT

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


    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    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…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now