COUNT XML节点并与gridview绑定 [英] COUNT XML node and bind with gridview

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

问题描述

你好每一个,

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

请有人建议关于如何计算XML文件中的节点并将其与网格视图绑定的任何想法



例如我有这个xml文件

我想要算上

员工= 2

名称= 2

年龄= 2





hello 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 how can I count the node in XML file and bind it with grid view

For example I have this xml file
I want to count the
employee=2
name = 2
age = 2
etc

<Employees>
     <Employee>
       <Name>Adams John</Name>
       <Age>35</Age>
       <Gender>M</Gender>
       <Salary>65000</Salary>
     </Employee>
     <Employee>
       <Name>Mary Jane</Name>
       <Age>35</Age>
       <Gender>F</Gender>
       <Salary>75000</Salary>
     </Employee>
   </Employee>





这是我的代码



this is my code

protected void Button1_Click(object sender, EventArgs e)
       {
           if (FileUpload1.HasFile)
           {
               string fileExt = System.IO.Path.GetExtension(FileUpload1.FileName);
               if (fileExt == ".xml")
               {
                   string Path = Server.MapPath("XMLFile1.xml");
                   FileUpload1.SaveAs(Path);
                   Label1.Visible = true;
                   Label1.Text = "File uploaded successfully.";
                   GridView1.DataBind();

               }
               else
               {
                   Label1.Visible = true;
                   Label1.Text = "Please Choose .XML File";
               }
           }





我的尝试:



之前我曾尝试过读取xml文件,但它不能正常工作,我使用了ReadXml但它无法正常工作,所以我认为它可能会计算节点而不读取文件。



What I have tried:

I tried before to read the xml file before it count but it does not work, I Used ReadXml but its not working, so I think it possibly to count the node without read the file.

推荐答案

试试这个,确保 xml 格式正确



try this, make sure that the xml is well formatted.

if (FileUpload1.HasFile)
            {
                string fileExt = System.IO.Path.GetExtension(FileUpload1.FileName);
                if (fileExt == ".xml")
                {
                    string Path = Server.MapPath("XMLFile1.xml");
                    FileUpload1.SaveAs(Path);
                    Label1.Visible = true;
                    Label1.Text = "File uploaded successfully.";
                    DataSet ds = new DataSet();
                    ds.ReadXml(Path);
                    if (ds.Tables.Count > 0)
                    {
                        int count = ds.Tables[0].Rows.Count;
                        DataTable dt = ds.Tables[0].Clone();
                        DataRow datarow = dt.NewRow();
                        foreach (DataColumn col in dt.Columns)
                            datarow[col] = count;
                        dt.Rows.Add(datarow);
                        GridView1.DataSource = dt;
                        GridView1.DataBind();
                    }
                }
                else
                {
                    Label1.Visible = true;
                    Label1.Text = "Please Choose .XML File";
                }
            }


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

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