Readxml并使用gridview绑定XML文件 [英] Readxml and bind XML file with gridview

查看:84
本文介绍了Readxml并使用gridview绑定XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Helloo每一个,

我是xml文件的新手,我正试图解决这个问题,但我正在努力解决这个问题

请有人建议任何关于它的想法



我有这个代码

当我运行这段代码时它会给我错误(ds.ReadXml(filePath); )



 受保护  void  Button1_Click( object  sender,EventArgs e)
{
if (FileUpload1.HasFile)
{
string filename = System.IO.Path.GetFileName(FileUpload1.FileName);
FileUpload1.SaveAs(Server.MapPath( 〜/ folder /)+ filename) ;
string filePath = 〜/ folder / + filename;
Label1.Text = 文件已成功上传。;
DataSet ds = new DataSet();
ds.ReadXml(filePath);
GridView1.Visible = true ;
GridView1.DataSource = ds;
GridView1.DataBind();
}
else
{
Label1.Text = 请选择文件。;
}
}





我的尝试:



我尝试将xml文件上传到文件夹

下载到文件夹后它应该读取xml文件并在Gridview中显示

解决方案

 使用 System.Data; 

受保护 void Page_Load(对象发​​件人,EventArgs e)
{
如果(!this.IsPostBack)
{
< span class =code-keyword> this .BindGrid();
}
}

私有 void BindGrid( )
{
使用(DataSet ds = new DataSet())
{
ds.ReadXml(Server.MapPath(& quot;〜/ Customers.xml& quot;));
GridView1.DataSource = ds;
GridView1.DataBind();
}
}

受保护 void OnPageIndexChanging( object sender,GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
this .BindGrid();
}







网站原始代码来自读取XML文件并将其绑定到ASP.Net中的GridView [ ^ ]


使用此代码



  string  fileName = System.IO.Path.GetFileName(FileUpload1.FileName); 
FileUpload1.SaveAs(Server.MapPath( 〜/ folder /)+ fileName) ;
string filePath = 〜 / folder / + filename;
string filePath = Path.Combine(Server.MapPath( 〜/ folder /),fileName); // 应获得完整路径
Label1.Text = 文件上传成功。;


看起来你可能会有一些XML文件,其结构不受DataSet类的支持,但是如果没有看到实际的XML文件,很难帮助你。



如果非常大,你不应该在这里发布,而是在文本编辑器中打开XML文件,并尝试删除冗余数据。尝试不时加载它,看看你是否还有问题。如果没有,那么你就知道问题所在。

如果它足够小,你可以用相关的XML数据更新你的问题。



还要在XML文件中查找递归嵌套。 DataSet不太喜欢。

示例:

 <   root  >  
< node >
< 名称 > Harry Hacker < / name >
< node >
< ; 名称 > 米老鼠< / name > ;
< / node >
< / node >
< / root >


Helloo every one,
I'm new to xml file and I'm try to solve this problem but i'm struggling with it
please can anyone suggest any idea about it

I have this code
when I run this code it give me error in (ds.ReadXml(filePath);)

protected void Button1_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                string filename = System.IO.Path.GetFileName(FileUpload1.FileName);
                FileUpload1.SaveAs(Server.MapPath("~/folder/") + filename);
                string filePath = "~/folder/" + filename;
                Label1.Text = "File uploaded successfully.";
                DataSet ds = new DataSet();
                ds.ReadXml(filePath);
                GridView1.Visible = true;
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
            else
            {
                Label1.Text = "Please select file.";
            }
        }



What I have tried:

I'm try to upload xml file to folder
after it download to folder it should read the xml file and display it in Gridview

解决方案

using System.Data;

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.BindGrid();
    }
}
 
private void BindGrid()
{
    using (DataSet ds = new DataSet())
    {
        ds.ReadXml(Server.MapPath(&quot;~/Customers.xml&quot;));
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
}
 
protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    this.BindGrid();
}




site that original code from Read XML File and bind it to GridView in ASP.Net[^]


use this code

string fileName = System.IO.Path.GetFileName(FileUpload1.FileName);
               FileUpload1.SaveAs(Server.MapPath("~/folder/") + fileName);
               string filePath = "~/folder/" + filename;
               string filePath = Path.Combine(Server.MapPath("~/folder/"), fileName); // should get full path
               Label1.Text = "File uploaded successfully.";


It looks like you might have some XML files with a structure that is not supported by the DataSet class, but it is difficult to help you without seeing the actual XML file.

If it is very large you shouldn't post it here, instead open the XML file in a text editor and try to remove redundant data. Try to load it from time to time to see if you still have the problem. If not, then you kind of know where the problem lies.
If it is small enough you can update your question with the relevant XML data.

Also look for recursive nesting inside the XML file. DataSet doesn't like that very much.
Example:

<root>
    <node>
        <name>Harry Hacker</name>
        <node>
            <name>Mickey Mouse</name>
        </node>
    </node>
</root>


这篇关于Readxml并使用gridview绑定XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆