Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have one ASP.net Project which extracts data from XML file and displays on webpage.

I have saved XML File in C drive
When I run this on Local host ...It Runs Successfully....!!!

While when I run this Online on my website ....then I shows error ...that could not find XML file path C:\ Form data

please tell me ....How to solve this ..


-Rectus
Posted
Comments
Sergey Alexandrovich Kryukov 8-Oct-13 13:42pm    
Why would you even assume it could be found there. The paths like "C:\" are not even legal.
—SA

1 solution

Web applications are being executed in sandboxed environment which prevents damaging the host systems. Your files can only be accessed if they are placed under the root directory configured for your site. For the location of the file in your host file system, you can call the method MapPath:
http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath.aspx[^].

And, come on, you server host may have no the idea even on what the "drive C:" is :-).

—SA
 
Share this answer
 
Comments
Member 10272175 9-Oct-13 8:14am    
Sir please tell so How can I specify path ?
Sergey Alexandrovich Kryukov 9-Oct-13 9:36am    
What do you mean by specify? You know better where could you put that XML file, but it should be withing your site's root directory.
You know this path relative to some ASP.NET page, but reading it requires absolute path, which you can get by calling MapPath.
—SA
Member 10272175 10-Oct-13 8:41am    
Sir my code for file path is
string pathToFiles = Server.MapPath(@"C:\Form Data.xml");

My project files saved in C drive
So How to give absolute path ? sir please help me
Member 10272175 10-Oct-13 9:57am    
My code is like sir

protected void Page_Load(object sender, EventArgs e)
{
string pathToFiles = Server.MapPath(@"C:\Form Data.xml");

Panel1.Visible = false;
Panel2.Visible = false;

Panel6.Visible = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
Panel1.Visible = true;
Panel2.Visible = true;

Panel6.Visible = true;
Button1.Visible = false;

XmlReader xmlRdr = XmlReader.Create(@"C:\Form Data.xml");



while (xmlRdr.Read())
{
if (xmlRdr.Name == "CompanyName")
{
Label13.Text = (Label13.Text == string.Empty) ? xmlRdr.ReadString() : Label13.Text;
}
Sergey Alexandrovich Kryukov 10-Oct-13 12:54pm    
This is not what MapPath is supposed to do. Do you really understand the concept of "relative path"? You need to use something like Server.MapPath("Form Data.xml"), Server.MapPath("./Form Data.xml").
Look at the examples: http://msdn.microsoft.com/en-us/library/ms524632%28v=vs.90%29.aspx.

Also, keep in mind: there are no situations where hard-coding of the file path can be useful.

—SA

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