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
Instead of
xmldoc.Load(HttpContext.Server.MapPath("~/Content/settings.xml"));
try with only
xmldoc.Load(Server.MapPath("~/Content/settings.xml"));
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With