Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Getting an error:
An exception of type 'System.IO.DirectoryNotFoundException' occurred in System.Xml.dll but was not handled in user code

Additional information: Could not find a part of the path 'C:\Program Files (x86)\IIS Express\~\Default.aspx'.
C#
protected void Button1_Click(object sender, System.EventArgs e)
{
    XmlDocument doc1 = new XmlDocument();
    doc1.Load("~/Default.aspx");
    XmlElement root = doc1.DocumentElement;
    XmlNodeList nodes = root.SelectNodes("/xml/xml1.xml");

    foreach (XmlNode node in nodes)
    {
        string temp1 = node["field1"].InnerText;
        string temp2 = node["field2"].InnerText;
        string temp3 = node["field3"].InnerText;

        Label1.Text = temp1;
        Label2.Text = temp2;
        Label3.Text = temp3;
    }

~/xml/xml1.xml
XML
<?xml version="1.0" encoding="utf-8" ?>
<sorts>
  <sort>
    <field1>11</field1>
    <field2>22</field2>
    <field3>33</field3>
  </sort>
</sorts>
Posted
Comments
Rob Philpott 11-Mar-15 9:01am    
The tilde is a Unix type convention, not sure it means anything to Windows. Looks like its treating it as a real directory name. That's why it can't resolve the directory.
Richard Deeming 11-Mar-15 10:10am    
In ASP.NET, the tilde represents an app-relative URL. However, it only works with methods or properties which are expecting a URL of this type.
Rob Philpott 11-Mar-15 12:22pm    
Ah well, there we go. Didn't know that, the web isn't my thing. Presumably that's what MapPath shown below sorts out.
teledexterus 11-Mar-15 9:07am    
doc1.Load("Default.aspx");
An exception of type 'System.IO.FileNotFoundException' occurred in System.Xml.dll but was not handled in user code

Additional information: Could not find file 'C:\Program Files (x86)\IIS Express\Default.aspx'.

Try this:
C#
protected void Button1_Click(object sender, EventArgs e)
{
    XmlDocument doc1 = new XmlDocument();
    doc1.Load(Server.MapPath("~/xml/xml1.xml"));

    foreach (XmlNode fieldNode in doc1.SelectSingleNode("/sorts/sort").ChildNodes)
    {
        switch (fieldNode.Name)
        {
            case "field1":
                Label1.Text = fieldNode.InnerText;
                break;
            case "field2":
                Label2.Text = fieldNode.InnerText;
                break;
            case "field3":
                Label3.Text = fieldNode.InnerText;
                break;
        }
    }
}
 
Share this answer
 
Comments
Richard Deeming 11-Mar-15 10:49am    
Down-vote countered. I've only given it a 4 because you didn't explain the solution, but otherwise a perfectly valid answer. :)
The first step would be to provide a correct path to the file you are trying to open.
Could not find a part of the path 'C:\Program Files (x86)\IIS Express\~\Default.aspx'

Isn't that explicit enough?
 
Share this answer
 
The XmlDocument doesn't know about ASP.NET's app-relative URLs. You need to pass the full physical path to the Load method.

You also need to specify the path to your XML file, not the path to your Default.aspx file.

You also need to provide the correct XPath expression to the Select method.
C#
string physicalPath = Server.MapPath("~/xml/xml1.xml");
doc1.Load(physicalPath);

XmlElement root = doc1.DocumentElement;
XmlNodeList nodes = root.SelectNodes("/sorts/sort");
 
Share this answer
 
Comments
Mario Z 11-Mar-15 10:44am    
Richard sorry to bother you, but have you downvoted my answer?
If yes then can you tell me why, is it because I answered few minutes behind you?
Richard Deeming 11-Mar-15 10:47am    
No, I haven't.

The two answers are very similar, but they were posted within minutes of each other, so it's obvious that you started writing yours before I posted mine. Also, the solution is rather obvious, so it's almost certain that multiple people would come up with the same answer.
Mario Z 11-Mar-15 10:50am    
Thank you for the fast reply, I was afraid that the same issue that I encountered in SO forum on few occasion is repeating here... I really don't like that.
Again sorry for bothering you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900