尝试从XMl文件中检索数据但是我在文件正在使用时收到错误(Trying to retrieve data froman XMl file but I get the error as file is

编程入门 行业动态 更新时间:2024-10-27 12:38:30
尝试从XMl文件中检索数据但是我在文件正在使用时收到错误(Trying to retrieve data froman XMl file but I get the error as file is in use)

我正在尝试从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)) { ... }

更多推荐

本文发布于:2023-08-02 20:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1379707.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   我在   错误   数据   XMl

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!