Click here to Skip to main content
15,900,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My query to a large Xml Database
C#
var query = DOC.Descendants("entry").Where(f => f.Element("url").Value.Contains("example"));

returns multiple xml Data
I display the data using a richtextbox
C#
foreach (XElement result in query)
    {richTextBox1.Text += Environment.NewLine + Environment.NewLine + result; }

One xml data looks like this:



XML
<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>


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:
C#
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];


        }      

But it only displays the last XmlData and not all the values.Please Help.Thanks in advance
Posted
Updated 22-Apr-16 8:12am
Comments
Herman<T>.Instance 22-Apr-16 4:45am    
dataGridView1.DataSource = ds;
Member 11273642 22-Apr-16 4:49am    
I did that and the datagridview doesnt load or show anything.
Herman<T>.Instance 22-Apr-16 5:22am    
dataGridView1.DataSource = ds.Tables; ?
Member 11273642 22-Apr-16 5:41am    
Nope doesnt work either.
Jawad Ahmed Tanoli 22-Apr-16 6:15am    
check your Dataset how many tables load in it.

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.
C#
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; }
        }
    }
}
 
Share this answer
 
v2
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.
C#
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; }
            }
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900