在datagridview中显示每个xml元素

时间:2019-02-28 19:34:25

标签: c# xml datagridview

我尝试在datagridview中显示每个“包”元素。但看起来它仅显示版本1.02。这是我的代码:

private void button1_Click(object sender, EventArgs e)
    {
        string path = Environment.CurrentDirectory;

        DataTable dt = new DataTable();
        dt.Columns.Add("version", typeof(string));
        dt.Columns.Add("sha1sum", typeof(string));
        dt.Columns.Add("url", typeof(string));
        dt.Columns.Add("ps3_system_ver", typeof(string));
        dt.Columns.Add("size", typeof(string));

        XmlDocument xDoc = new XmlDocument();
        xDoc.Load(path + @"\\temp\\BCES01893-ver.xml");
        XDocument doc = XDocument.Load(path + @"\\temp\\BCES01893-ver.xml");

        XmlNodeList p = xDoc.GetElementsByTagName("package");
        for (int i = 0; i < p.Count; i++)
        {
            if (p.Count > 0)
            {
                foreach (XElement addresdm in doc.Descendants().Where(x => x.Name.LocalName == "tag"))
                {

                    XElement avee = addresdm.Descendants("package").FirstOrDefault();
                    dt.Rows.Add(new object[] {
                (string)avee.Attribute("version"),
                (string)avee.Attribute("sha1sum"),
                (string)avee.Attribute("url"),
                (string)avee.Attribute("ps3_system_ver"),
                (string)avee.Attribute("size"),
                });

                }
            }
            dataGridView1.DataSource = dt;
        }
    }

xml文件:

<titlepatch status="alive" titleid="BCES01893">
  <tag name="BCES01893_T359" popup="true" signoff="true" min_system_ver="03.60">
    <package version="01.02" size="1315387312" sha1sum="d68d0584aa30b6d02a7aee95236f91ef43fdc11e" url="http://b0.ww.np.dl.playstation.net/tppkg/np/BCES01893/BCES01893_T359/b80952de9329a9ba/EP9001-BCES01893_00-0000000000000000-A0102-V0101-PE.pkg" ps3_system_ver="04.4000"/>
    <package version="01.03" size="102857792" sha1sum="7b1c6b9dd621ed9a5ae130737bfa5a4dd386db66" url="http://b0.ww.np.dl.playstation.net/tppkg/np/BCES01893/BCES01893_T359/b80952de9329a9ba/EP9001-BCES01893_00-0000000000000000-A0103-V0100-PE.pkg" ps3_system_ver="04.4000"/>
    <package version="01.04" size="223699280" sha1sum="4cedbc07c384b0f5556d5f0697b03c81376fd89d" url="http://b0.ww.np.dl.playstation.net/tppkg/np/BCES01893/BCES01893_T359/b80952de9329a9ba/EP9001-BCES01893_00-0000000000000000-A0104-V0100-PE.pkg" ps3_system_ver="04.4000"/>
    </package>
  </tag>
</titlepatch>

结果:https://i.ibb.co/cQJRFzj/Untitled.png

我如何使其正确显示?

1 个答案:

答案 0 :(得分:1)

您只会得到最后一项,因为以下内容在for循环内:dataGridView1.DataSource = dt;在for循环外移动。

这是我的代码,简单一些。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Data;

namespace ConsoleApplication103
{
    class Program
    {

        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {

            DataTable dt = new DataTable();
            dt.Columns.Add("version", typeof(string));
            dt.Columns.Add("sha1sum", typeof(string));
            dt.Columns.Add("url", typeof(string));
            dt.Columns.Add("ps3_system_ver", typeof(string));
            dt.Columns.Add("size", typeof(string));

            XDocument doc = XDocument.Load(FILENAME);

            foreach (XElement addresdm in doc.Descendants("package"))
            {
                 dt.Rows.Add(new object[] {
                    (string)addresdm.Attribute("version"),
                    (string)addresdm.Attribute("sha1sum"),
                    (string)addresdm.Attribute("url"),
                    (string)addresdm.Attribute("ps3_system_ver"),
                    (string)addresdm.Attribute("size"),
                });
            }

        }
    }


}