如何在datagridview中放置多个XML数据 [英] How to put multiple XML data in datagridview

查看:66
本文介绍了如何在datagridview中放置多个XML数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对大型Xml数据库的查询

My query to a large Xml Database

var query = DOC.Descendants("entry").Where(f => f.Element("url").Value.Contains("example"));



返回多个xml数据

我使用richtextbox显示数据


returns multiple xml Data
I display the data using a richtextbox

foreach (XElement result in query)
    {richTextBox1.Text += Environment.NewLine + Environment.NewLine + result; }



一个xml数据如下所示:








One xml data looks like this:



<entry>
<url><![CDATA[http://triada.kh.ua/made/?email=abuse@example.com</url>
<phish_id>3779980</phish_id>
<phish_detail_url>http://www.phishtank.com/phish_detail.php?phish_id=3779980</phish_detail_url>
<details>
<detail>
  <ip_address>93.190.41.34</ip_address>
  <cidr_block>93.190.40.0/21</cidr_block>
  <announcing_network>6849</announcing_network>
  <rir>ripencc</rir>
  <detail_time>2016-01-24T01:00:58+00:00</detail_time>
</detail>
</details>
<submission>
<submission_time>2016-01-22T22:42:56+00:00</submission_time>
</submission>
<verification>
<verified>yes</verified>
<verification_time>2016-03-28T14:15:01+00:00</verification_time>
</verification>
<status>
<online>yes</online>
</status>
<target>Internal Revenue Service</target>
</entry>





你可以看到它相当复杂,一些Children节点具有包含值的其他子节点。我的问题是:

1)如何将所有这些值整齐地放入Datagridview中。

2)xml的这种确切格式有多种。我可以添加每个都作为Datagrid中的新列。

3)我如何提高性能,因为我的查询可能会返回50多个这样的xml数据。



请帮帮我。提前谢谢。



我尝试过的:



我尝试过以下代码:



As you can see its rather complex,some of the Children nodes have other children that contain values. My Questions are :
1) How can i put all this values neatly into a Datagridview.
2) There are multiple of this exact format of xml.How can i add each as a new column in the Datagrid.
3)How can i improve performance as my queries may return more than 50 of this xml data.

Please Help me.Thank you in Advance.

What I have tried:

I have tried the following code:

foreach (XElement result in query)

        {

            string display = result.ToString();

            XmlReader xmlFile;
            xmlFile = XmlReader.Create(new StringReader( display));
            DataSet ds = new DataSet();

            ds.ReadXml(xmlFile);

                dataGridView1.DataSource = ds.Tables[0];


        }      



但是它只显示最后的XmlData而不是所有的值。请帮助。提前谢谢


But it only displays the last XmlData and not all the values.Please Help.Thanks in advance

推荐答案

我无法回答您的所有问题,但我认为以下代码可以帮助您入门。

您需要一张<$ c的表格$ c> DataGridView ,上面有一个按钮。

临时类用于存储适合在 DataGridView

XmlDriver 类还有一些用于序列化XML的例程,但是在示例中没有使用它。

I can not answer all your questions, but I think the code below will get you started.
You need a Form with a DataGridView and a button on it.
A temporary class is used to store the data suitable for display in a DataGridView.
The main XmlDriver class also has some routines for serializing to and from XML, but in the example this is not used.
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace TestSerialization2
{
    /// <summary>
    /// Test DataGridView with complex class.
    /// Allow serializing in XML format.
    /// </summary>
    public partial class Form2 : Form
    {
        public BindingList<XmlDriverForGrid> bindingList;

        public Form2()
        {
            InitializeComponent();
        }

        private void buttonLoadXml_Click(object sender, EventArgs e)
        {
            bindingList = new BindingList<XmlDriverForGrid>();
            this.AddTestData();
        }

        private void AddTestData()
        {
            // Add rows with test data (this is only for testing normally data is read from an .xml file)
            var item = new XmlDriver("Brand1", "Test model1");
            item.Xptz = new XmlDriver.Ptz();   // Xptz is a subclass.

            // Convert the data to grid format.
            var itemForGrid = new XmlDriverForGrid(item);
            bindingList.Add(itemForGrid);

            item = new XmlDriver("Brand2", "Test model2");
            item.Xptz = new XmlDriver.Ptz(true, "Test zoomcommand2");   // Xptz is a subclass.

            // Convert the data to grid format.
            var itemForGrid2 = new XmlDriverForGrid(item);
            bindingList.Add(itemForGrid2);

            this.dataGridView1.DataSource = bindingList;
        }

        /// <summary>
        /// Class containing a subclass: Ptz.
        /// </summary>
        public class XmlDriver
        {
            public XmlDriver()
            {
                // needed for serialization.
            }

            public XmlDriver(string brand, string model)
            {
                this.Xbrand = brand;
                this.Xmodel = model;
            }

            public string Xbrand { get; set; }
            public string Xmodel { get; set; }
            public Ptz Xptz { get; set; }

            /// <summary>
            /// Saves to an xml file
            /// </summary>
            /// <param name="FileName">File path of the new xml file</param>
            public void Save(string FileName)
            {
                using (var writer = new System.IO.StreamWriter(FileName))
                {
                    var serializer = new XmlSerializer(this.GetType());
                    serializer.Serialize(writer, this);
                    writer.Flush();
                }
            }

            /// <summary>
            /// Load an object from an xml file
            /// </summary>
            /// <param name="FileName">Xml file name</param>
            /// <returns>The object created from the xml file</returns>
            public void Load(string FileName)
            {
                XmlDriver temp;

                using (var stream = System.IO.File.OpenRead(FileName))
                {
                    var serializer = new XmlSerializer(typeof(XmlDriver));
                    temp = serializer.Deserialize(stream) as XmlDriver;
                }

                this.Xbrand = temp.Xbrand;
                this.Xmodel = temp.Xmodel;
                this.Xptz = temp.Xptz;
            }

            public class Ptz
            {
                public Ptz()
                {
                    // needed for serialization.
                }

                public Ptz(bool enabled, string zoomCommand)
                {
                    this.Enabled = enabled;
                    this.ZoomCommand = zoomCommand;
                }

                public bool Enabled { get; set; }
                public string ZoomCommand { get; set; }
            }
        }

        public class XmlDriverForGrid
        {
            public XmlDriverForGrid(XmlDriver xmlDriver)
            {
                this.Xbrand = xmlDriver.Xbrand;
                this.Xmodel = xmlDriver.Xmodel;
                this.Enabled = xmlDriver.Xptz.Enabled;
                this.ZoomCommand = xmlDriver.Xptz.ZoomCommand;
            }

            public string Xbrand { get; set; }
            public string Xmodel { get; set; }
            //public Ptz Xptz { get; set; }
            public bool Enabled { get; set; }
            public string ZoomCommand { get; set; }
        }
    }
}


另一个解决方案可能是使用两个DataGridViews来使用Master-Detail视图。

您需要一个带有 dataGridView1 (主)和另一个名为 dataGridViewDetails 的表单。 />
当用户在主网格中选择一行时,详细信息将显示在详细信息网格中。

Another solution might be to use a Master-Detail view using two DataGridViews.
You need a form with a dataGridView1 (master) and another named dataGridViewDetails.
When the user selects a row in the master grid, the details will be displayed in the details grid.
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace TestSerialization2
{
    /// <summary>
    /// Test master details DataGridView with complex class.
    /// Show details in dataGridViewDetails when row is selected.
    /// Allow serializing in XML format.
    /// </summary>
    public partial class Form2 : Form
    {
        public BindingList<XmlDriver> bindingList;
        public BindingList<XmlDriver.Ptz> bindingListDetail;
        private bool skipFirst;

        public Form2()
        {
            InitializeComponent();
        }

        private void buttonLoadXml_Click(object sender, EventArgs e)
        {
            bindingList = new BindingList<XmlDriver>();
            this.AddTestData();
            this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.RowEnter += this.dataGridView1_RowEnter;
        }

        private void AddTestData()
        {
            // Add rows with test data (this is only for testing normally data is read from an .xml file)
            var item = new XmlDriver("Brand1", "Test model1");
            item.Xptz = new XmlDriver.Ptz();   // Xptz is a subclass.
            bindingList.Add(item);

            item = new XmlDriver("Brand2", "Test model2");
            item.Xptz = new XmlDriver.Ptz(true, "Test zoomcommand2");   // Xptz is a subclass.
            //item.Save("test1.xml");
            bindingList.Add(item);

            this.dataGridView1.DataSource = bindingList;
        }

        /// <summary>
        /// When user selects a row, extract the Ptz class and show the details in dataGridViewDetails. 
        /// </summary>
        private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (skipFirst)
            {
                this.bindingListDetail = new BindingList<XmlDriver.Ptz>();
                this.bindingListDetail.AllowNew = false;
                var ptz = this.bindingList[e.RowIndex].Xptz;
                this.bindingListDetail.Add(ptz);
                this.dataGridViewDetails.DataSource = bindingListDetail;
            }

            skipFirst = true;
        }

        /// <summary>
        /// Class containing a subclass: Ptz.
        /// Note that Ptz is used in field Xptz, which is not a property.
        /// Only properties will be displayed in the master data grid.
        /// </summary>
        public class XmlDriver
        {
            public XmlDriver()
            {
                // needed for serialization.
            }

            public XmlDriver(string brand, string model)
            {
                this.Xbrand = brand;
                this.Xmodel = model;
            }

            // Properties for display in data grid.
            public string Xbrand { get; set; }
            public string Xmodel { get; set; }

            // Not a property !
            public Ptz Xptz;

            /// <summary>
            /// Saves to an xml file
            /// </summary>
            /// <param name="FileName">File path of the new xml file</param>
            public void Save(string FileName)
            {
                using (var writer = new System.IO.StreamWriter(FileName))
                {
                    var serializer = new XmlSerializer(this.GetType());
                    serializer.Serialize(writer, this);
                    writer.Flush();
                }
            }

            /// <summary>
            /// Load an object from an xml file
            /// </summary>
            /// <param name="FileName">Xml file name</param>
            /// <returns>The object created from the xml file</returns>
            public void Load(string FileName)
            {
                XmlDriver temp;

                using (var stream = System.IO.File.OpenRead(FileName))
                {
                    var serializer = new XmlSerializer(typeof(XmlDriver));
                    temp = serializer.Deserialize(stream) as XmlDriver;
                }

                this.Xbrand = temp.Xbrand;
                this.Xmodel = temp.Xmodel;
                this.Xptz = temp.Xptz;
            }

            public class Ptz
            {
                public Ptz()
                {
                    // needed for serialization.
                }

                public Ptz(bool enabled, string zoomCommand)
                {
                    this.Enabled = enabled;
                    this.ZoomCommand = zoomCommand;
                }

                public bool Enabled { get; set; }
                public string ZoomCommand { get; set; }
            }
        }
    }
}


这篇关于如何在datagridview中放置多个XML数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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