我正在尝试从Xml中检索数据。 我是编程的新手所以请原谅我。
protected void Page_Load(object sender, EventArgs e) { string MyXmlFile= @"E:\\Programming stuff\\Work\\website\\XMLFile.xml"; DataSet ds= new DataSet(); System.IO.FileStream MyReadXml= new System.IO.FileStream(MyXmlFile, System.IO.FileMode.Open); ds.ReadXml(MyReadXml); DataGrid DataGrid1 = new DataGrid(); DataGrid1.DataSource = ds; DataGrid1.DataBind(); }我在浏览器上遇到的错误是:
“该进程无法访问文件'E:\ Programming stuff \ Work \ website \ XMLFile.xml',因为它正由另一个进程使用。”
你能帮我识别一下正在访问该文件的其他进程吗?
编辑:更改代码后:
protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { string MyXmlFile= Server.MapPath("~/XMLFile.xml"); using(System.IO.FileStream MyReadXml= new System.IO.FileStream(MyXmlFile,System.IO.FileMode.Open)); { DataSet ds= new DataSet(); ds.ReadXml(MyReadXml); DataGrid DataGrid1 = new DataGrid(); DataGrid1.DataSource = ds; DataGrid1.DataBind(); PlaceHolder1.Controls.Add(DataGrid1); } } }错误:“当前上下文中不存在名称'MyReadXml'”
I'm trying to retrieve data from an Xml. I'm a newbie into programming so please pardon me.
protected void Page_Load(object sender, EventArgs e) { string MyXmlFile= @"E:\\Programming stuff\\Work\\website\\XMLFile.xml"; DataSet ds= new DataSet(); System.IO.FileStream MyReadXml= new System.IO.FileStream(MyXmlFile, System.IO.FileMode.Open); ds.ReadXml(MyReadXml); DataGrid DataGrid1 = new DataGrid(); DataGrid1.DataSource = ds; DataGrid1.DataBind(); }The error I get on the browser is:
"The process cannot access the file 'E:\Programming stuff\Work\website\XMLFile.xml' because it is being used by another process."
Can you help me identify which is that other process that is accessing the file?
Edit: After the changes to the code:
protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { string MyXmlFile= Server.MapPath("~/XMLFile.xml"); using(System.IO.FileStream MyReadXml= new System.IO.FileStream(MyXmlFile,System.IO.FileMode.Open)); { DataSet ds= new DataSet(); ds.ReadXml(MyReadXml); DataGrid DataGrid1 = new DataGrid(); DataGrid1.DataSource = ds; DataGrid1.DataBind(); PlaceHolder1.Controls.Add(DataGrid1); } } }Error: "The name 'MyReadXml' does not exist in the current context"
最满意答案
始终close并dispose流(尝试使用块)。 重新启动你的appserver(werserver),看看会发生什么?
if(!IsPostBack) { string MyXmlFile= @"E:\\Programming stuff\\Work\\website\\XMLFile.xml"; using(System.IO.FileStream MyReadXml=System.IO.File.OpenRead(MyXmlFile)) { DataSet ds= new DataSet(); ds.ReadXml(MyReadXml); //Add DataGrid control markup in .aspx. DataGrid1.DataSource = ds.Tables[0]; DataGrid1.DataBind(); } }注意:如果XMLFile.xml放在website的根目录下,则使用Server.MapPath()方法从虚拟路径获取绝对文件路径。
string MyXmlFile= Server.MapPath("~/XMLFile.xml");如果要以编程方式添加ASP.NET服务器控件,请将PlaceHolder控件添加到.aspx文件中,并调用PlaceControl1.Controls.Add(DataGrid1)方法。
string MyXmlFile= @"E:\\Programming stuff\\Work\\website\\XMLFile.xml"; using(System.IO.FileStream MyReadXml=System.IO.File.OpenRead(MyXmlFile)) { DataSet ds= new DataSet(); ds.ReadXml(MyReadXml); DataGrid DataGrid1=new DataGrid(); DataGrid1.DataSource = ds.Tables[0]; DataGrid1.DataBind(); PlaceHolder1.Controls.Add(DataGrid1); }编辑:
我仍然得到错误“MyReadXml不存在”我做错了什么?
你已经终止了使用块。 请删除分号。
using(System.IO.FileStream MyReadXml=new System.IO.FileStream(MyXmlFile,System.IO.FileMode.Open)) { ... }Always close and dispose the stream (try using block). restart your appserver (werserver) and see what happen?
if(!IsPostBack) { string MyXmlFile= @"E:\\Programming stuff\\Work\\website\\XMLFile.xml"; using(System.IO.FileStream MyReadXml=System.IO.File.OpenRead(MyXmlFile)) { DataSet ds= new DataSet(); ds.ReadXml(MyReadXml); //Add DataGrid control markup in .aspx. DataGrid1.DataSource = ds.Tables[0]; DataGrid1.DataBind(); } }NOTE: If XMLFile.xml is placed under the root of website then use Server.MapPath() method to get absolute file path from virtual path.
string MyXmlFile= Server.MapPath("~/XMLFile.xml");If you want to add ASP.NET server control programatically then add PlaceHolder control into .aspx file and call the PlaceControl1.Controls.Add(DataGrid1) method.
string MyXmlFile= @"E:\\Programming stuff\\Work\\website\\XMLFile.xml"; using(System.IO.FileStream MyReadXml=System.IO.File.OpenRead(MyXmlFile)) { DataSet ds= new DataSet(); ds.ReadXml(MyReadXml); DataGrid DataGrid1=new DataGrid(); DataGrid1.DataSource = ds.Tables[0]; DataGrid1.DataBind(); PlaceHolder1.Controls.Add(DataGrid1); }EDIT:
I still get the error as "MyReadXml does not exist" Did I do anything wrong?
You've terminated the using block. Please remove the semi-colon.
using(System.IO.FileStream MyReadXml=new System.IO.FileStream(MyXmlFile,System.IO.FileMode.Open)) { ... }更多推荐
发布评论