3

I am trying to load up an XML document in MVC in the "HomeController"

I want this document to load up in everything under the /Home/ directory so have my class:

public HomeController()
        {  }

And inside this I have the code that I want to connect to the XML with:

//Now set up the config xml read
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.Load(HttpContext.Server.MapPath("~/Content/settings.xml"));
        XmlNodeList settings = xmldoc.SelectNodes("/settings");
        XmlNodeList defaults = xmldoc.GetElementsByTagName("default");
        foreach (XmlNode node in defaults)
        {
            string def_WebPageName = node["WebPageName "].InnerText;
        }

Structure of the XML:

<settings>
<defaults>
  <WebPageName>blah</WebPageName>
</defaults>

I cannot seem to locate theXML file, keep getting a "Object set to null reference" error

5
  • 1
    What line causes the null ref exception? If xmldoc.Load couldn't find the file you would get some kind of IO exception not a null ref. Commented Sep 9, 2011 at 9:06
  • maybe the Path is not correct. xmldoc.Load(HttpContext.Server.MapPath("~/Content/settings.xml")); Commented Sep 9, 2011 at 9:12
  • Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error: Line 29: //Now set up the config xml read Line 30: XmlDocument xmldoc = new XmlDocument(); Line 31: xmldoc.Load(Server.MapPath("~/Content/settings.xml")); Commented Sep 9, 2011 at 9:18
  • using the debugger, try to find what is null. If required, refactor the code to use a variable like this : var filePath = HttpContext.Server.MapPath("~/Content/settings.xml"); xmldoc.Load(filePath); in order to check if the file is actually found, also, which line is the 29th ? Commented Sep 9, 2011 at 9:37
  • For some reason, httpContext is null when I inspect it. line 29 is just a comment. "//Now set up the config xml read" Commented Sep 9, 2011 at 9:43

2 Answers 2

6

Instead of

xmldoc.Load(HttpContext.Server.MapPath("~/Content/settings.xml"));

try with only

xmldoc.Load(Server.MapPath("~/Content/settings.xml"));
Sign up to request clarification or add additional context in comments.

Comments

1

If it is a Web app you get somthing like http://yoursite/Content/settings.xml. Check whether this file path exists on the web server. If it is a WinForms app use ExecutionPath or Environment variables to get the path you need.

You should also use something like if(File.Exists(yourFilePathHere))

2 Comments

Just checked from the browser, and the file is accessible. I think it may have something to do with how I am calling the file?
So try to use a stream from webrequest instead of the string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.