未知昵称
2018-07-19 10:26
采纳率: 89.7%
浏览 1.6k

C#读取xml文件数据。显示在datagridview控件问题

图片说明

如上图所示框起来的是xml文件的一个子节点内容,这个子节点中包括了不同结构的子节点数据。可以仔细看下这个xml结构,现在我想把这个节点的数据按照下面的格式显示在文本框以及datagridview中。其他同等节点类似,具体代码该怎么写呢?

图片说明

xml文件下载地址

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

9条回答 默认 最新

  • threenewbee 2018-07-19 13:25
    已采纳

    图片说明

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • threenewbee 2018-07-19 10:42

    图片说明

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Xml;
    
    namespace Q694896
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            public Form1(XmlNode pnode) : this()
            {
                parentNode = pnode;
                if (this.Height > 160)
                {
                    this.Height -= 40;
                    this.Width -= 60;
                }
            }
    
            string clickme = "click me";
    
            XmlNode parentNode;
    
            class DataItem
            {
                public string ShortName { get; set; }
                public string Category { get; set; }
                public string BaseTypeRef { get; set; }
                public object Node { get; set; }
                public DataItem(string a, string b, string c)
                {
                    ShortName = a;
                    Category = b;
                    BaseTypeRef = c;
                }
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                List<DataItem> data = new List<DataItem>();
                IEnumerable<XmlNode> nodes;
                if (parentNode == null)
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(@"TunerServiceInterface.arxml");
                    nodes = doc.GetElementsByTagName("IMPLEMENTATION-DATA-TYPE").Cast<XmlNode>();
                }
                else
                {
                    nodes = parentNode.ChildNodes.Cast<XmlNode>().Where(x => x.Name == "IMPLEMENTATION-DATA-TYPE-ELEMENT");
                    textBox1.Text = parentNode.ParentNode.ChildNodes.Cast<XmlNode>().FirstOrDefault(x => x.Name == "SHORT-NAME").InnerText;
                    textBox2.Text = parentNode.ParentNode.ChildNodes.Cast<XmlNode>().FirstOrDefault(x => x.Name == "CATEGORY").InnerText;
                }
                foreach (XmlNode node in nodes)
                {
                    string sn = "";
                    try
                    {
                        sn = node.ChildNodes.Cast<XmlNode>().FirstOrDefault(x => x.Name == "SHORT-NAME").InnerText;
                        var c = node.ChildNodes.Cast<XmlNode>().FirstOrDefault(x => x.Name == "CATEGORY").InnerText;
                        var btf = node.ChildNodes.Cast<XmlNode>().FirstOrDefault(x => x.Name == "SW-DATA-DEF-PROPS").InnerText;
                        data.Add(new DataItem(sn, c, btf) { Node = node });
                    }
                    catch
                    {
                        if (node.ChildNodes.Cast<XmlNode>().Any(x => x.Name == "SUB-ELEMENTS"))
                        {
                            data.Add(new DataItem(sn, clickme, "") { Node = node.ChildNodes.Cast<XmlNode>().FirstOrDefault(x => x.Name == "SUB-ELEMENTS") });
                        }
                    }
                }
                dataGridView1.DataSource = data;
            }
    
            private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                string s = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                if (s == clickme)
                {
                    var f = new Form1((XmlNode)dataGridView1.Rows[e.RowIndex].Cells[dataGridView1.Rows[e.RowIndex].Cells.Count - 1].Value);
                    f.StartPosition = FormStartPosition.CenterParent;
                    f.ShowDialog();
                }
            }
        }
    }
    
    评论
    解决 无用
    打赏 举报
  • threenewbee 2018-07-19 10:43
     namespace Q694896
    {
        partial class Form1
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Windows Form Designer generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.label1 = new System.Windows.Forms.Label();
                this.textBox1 = new System.Windows.Forms.TextBox();
                this.label2 = new System.Windows.Forms.Label();
                this.textBox2 = new System.Windows.Forms.TextBox();
                this.dataGridView1 = new System.Windows.Forms.DataGridView();
                ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
                this.SuspendLayout();
                // 
                // label1
                // 
                this.label1.AutoSize = true;
                this.label1.Location = new System.Drawing.Point(12, 18);
                this.label1.Name = "label1";
                this.label1.Size = new System.Drawing.Size(56, 13);
                this.label1.TabIndex = 0;
                this.label1.Text = "shortname";
                // 
                // textBox1
                // 
                this.textBox1.Location = new System.Drawing.Point(74, 15);
                this.textBox1.Name = "textBox1";
                this.textBox1.Size = new System.Drawing.Size(133, 20);
                this.textBox1.TabIndex = 1;
                // 
                // label2
                // 
                this.label2.AutoSize = true;
                this.label2.Location = new System.Drawing.Point(230, 18);
                this.label2.Name = "label2";
                this.label2.Size = new System.Drawing.Size(51, 13);
                this.label2.TabIndex = 2;
                this.label2.Text = "catregory";
                // 
                // textBox2
                // 
                this.textBox2.Location = new System.Drawing.Point(287, 15);
                this.textBox2.Name = "textBox2";
                this.textBox2.Size = new System.Drawing.Size(133, 20);
                this.textBox2.TabIndex = 3;
                // 
                // dataGridView1
                // 
                this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
                this.dataGridView1.Location = new System.Drawing.Point(14, 50);
                this.dataGridView1.Name = "dataGridView1";
                this.dataGridView1.Size = new System.Drawing.Size(733, 390);
                this.dataGridView1.TabIndex = 4;
                this.dataGridView1.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellClick);
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(759, 452);
                this.Controls.Add(this.dataGridView1);
                this.Controls.Add(this.textBox2);
                this.Controls.Add(this.label2);
                this.Controls.Add(this.textBox1);
                this.Controls.Add(this.label1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.Load += new System.EventHandler(this.Form1_Load);
                ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
                this.ResumeLayout(false);
                this.PerformLayout();
    
            }
    
            #endregion
    
            private System.Windows.Forms.Label label1;
            private System.Windows.Forms.TextBox textBox1;
            private System.Windows.Forms.Label label2;
            private System.Windows.Forms.TextBox textBox2;
            private System.Windows.Forms.DataGridView dataGridView1;
    
        }
    }
    
    
    
    评论
    解决 无用
    打赏 举报
  • threenewbee 2018-07-19 11:06

    图片说明

     using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Xml;
    
    namespace Q694896
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            public Form1(XmlNode pnode) : this()
            {
                parentNode = pnode;
                if (this.Height > 160)
                {
                    this.Height -= 40;
                    this.Width -= 60;
                }
            }
    
            string clickme = "click me";
    
            XmlNode parentNode;
    
            class DataItem
            {
                public string ShortName { get; set; }
                public string Category { get; set; }
                public string BaseTypeRef { get; set; }
                public object Node { get; set; }
                public DataItem(string a, string b, string c)
                {
                    ShortName = a;
                    Category = b;
                    BaseTypeRef = c;
                }
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                List<DataItem> data = new List<DataItem>();
                IEnumerable<XmlNode> nodes;
                if (parentNode == null)
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(@"TunerServiceInterface.arxml");
                    nodes = doc.GetElementsByTagName("IMPLEMENTATION-DATA-TYPE").Cast<XmlNode>();
                }
                else
                {
                    nodes = parentNode.ChildNodes.Cast<XmlNode>().Where(x => x.Name == "IMPLEMENTATION-DATA-TYPE-ELEMENT");
                    try
                    {
                        textBox1.Text = "";
                        textBox2.Text = "";
                        textBox1.Text = parentNode.ParentNode.ChildNodes.Cast<XmlNode>().FirstOrDefault(x => x.Name == "SHORT-NAME").InnerText;
                        textBox2.Text = parentNode.ParentNode.ChildNodes.Cast<XmlNode>().FirstOrDefault(x => x.Name == "CATEGORY").InnerText;
                    }
                    catch { }
                }
                foreach (XmlNode node in nodes)
                {
                    string sn = "";
                    try
                    {
                        sn = node.ChildNodes.Cast<XmlNode>().FirstOrDefault(x => x.Name == "SHORT-NAME").InnerText;
                        var c = node.ChildNodes.Cast<XmlNode>().FirstOrDefault(x => x.Name == "CATEGORY").InnerText;
                        var btf = node.ChildNodes.Cast<XmlNode>().FirstOrDefault(x => x.Name == "SW-DATA-DEF-PROPS").InnerText;
                        data.Add(new DataItem(sn, c, btf) { Node = node });
                    }
                    catch
                    {
                        if (node.ChildNodes.Cast<XmlNode>().Any(x => x.Name == "SUB-ELEMENTS"))
                        {
                            data.Add(new DataItem(sn, clickme, "") { Node = node.ChildNodes.Cast<XmlNode>().FirstOrDefault(x => x.Name == "SUB-ELEMENTS") });
                        }
                    }
                }
                dataGridView1.DataSource = data;
            }
    
            private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                if (e.ColumnIndex <= 0) return;
                string s = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                if (s == clickme)
                {
                    //var f = new Form1((XmlNode)dataGridView1.Rows[e.RowIndex].Cells[dataGridView1.Rows[e.RowIndex].Cells.Count - 1].Value);
                    //f.StartPosition = FormStartPosition.CenterParent;
                    //f.ShowDialog();
                    this.parentNode = (XmlNode)dataGridView1.Rows[e.RowIndex].Cells[dataGridView1.Rows[e.RowIndex].Cells.Count - 1].Value;
                    Form1_Load(this, null);
                }
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                string s = null;
                try
                {
                    this.parentNode = this.parentNode.ParentNode.ParentNode;
                    s = parentNode.ParentNode.ChildNodes.Cast<XmlNode>().FirstOrDefault(x => x.Name == "CATEGORY").InnerText;
                }
                catch
                {
                    this.parentNode = null;
                }
                Form1_Load(this, null);
            }
        }
    }
    
    
    评论
    解决 无用
    打赏 举报
  • threenewbee 2018-07-19 13:23
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Xml;
    using System.Collections;
    
    namespace Q695201
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(@"TunerServiceInterface.arxml");
                var root = new Node(doc.ChildNodes[1].ChildNodes[1].ChildNodes[0].ChildNodes[1].ChildNodes[1]);
                TreeNode tn = new TreeNode() { Text = root.ShortName, Tag = root };
                treeView1.Nodes.Add(tn);
                LoadTV(tn);
                treeView1.ExpandAll();
                treeView1.AfterSelect += new TreeViewEventHandler(treeView1_AfterSelect);
                treeView1.SelectedNode = tn;
            }
    
            void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
            {
                Node node = e.Node.Tag as Node;
    
                textBox1.Text = node.ShortName;
                textBox2.Text = node.Category;
                List<DGVItem> list = new List<DGVItem>();
                foreach (Node n in node.Children)
                {
                    DGVItem item = new DGVItem();
                    item.shortname = n.ShortName;
                    item.category = n.Category;
                    Node n1 = n.Children.Cast<Node>().FirstOrDefault();
                    if (n1 != null)
                    {
                        item.shortname1 = n1.ShortName;
                        item.category1 = n1.Category;
                        item.arraysize = n1.ArraySuze;
                        item.arraysizesemantic = n1.ArraySizeSemantic;
                        item.basetyperef = n1.BaseTypeRef;
                    }
                    list.Add(item);
                }
                DataGridView dgv = new DataGridView();
                dgv.Dock = DockStyle.Fill;
                panel1.Controls.Clear();
                panel1.Controls.Add(dgv);
                dgv.DataSource = list;
            }
    
            private void LoadTV(TreeNode tn)
            {
                Node n = tn.Tag as Node;
                if (n != null)
                {
                    foreach (Node item in n.Children)
                    {
                        TreeNode tn1 = new TreeNode() { Text = item.ShortName, Tag = item };
                        tn.Nodes.Add(tn1);
                        LoadTV(tn1);
                    }
                }
            }
        }
    
        class Node
        {
            private XmlNode _base;
    
            public Node(XmlNode baseNode)
            {
                _base = baseNode;
            }
    
            public NodeCollections Children
            {
                get
                {
                    var sub = _base.ChildNodes.Cast<XmlNode>().FirstOrDefault(x => x.Name == "SUB-ELEMENTS");
                    if (sub == null)
                        sub = _base.ChildNodes.Cast<XmlNode>().FirstOrDefault(x => x.Name == "ELEMENTS");
                    return new NodeCollections(sub);
                }
            }
    
            public string ShortName
            {
                get
                {
                    var node = _base.ChildNodes.Cast<XmlNode>().FirstOrDefault(x => x.Name == "SHORT-NAME");
                    if (node != null) return node.InnerText; else return "";
                }
            }
    
            public string Category
            {
                get
                {
                    var node = _base.ChildNodes.Cast<XmlNode>().FirstOrDefault(x => x.Name == "CATEGORY");
                    if (node != null) return node.InnerText; else return "";
                }
            }
    
            public string BaseTypeRef
            {
                get
                {
                    var node = _base.ChildNodes.Cast<XmlNode>().FirstOrDefault(x => x.Name == "SW-DATA-DEF-PROPS");
                    if (node != null) return node.InnerText; else return "";
                }
            }
    
            public string ArraySuze
            {
                get
                {
                    var node = _base.ChildNodes.Cast<XmlNode>().FirstOrDefault(x => x.Name == "ARRAY-SIZE");
                    if (node != null) return node.InnerText; else return "";
                }
            }
            public string ArraySizeSemantic
            {
                get
                {
                    var node = _base.ChildNodes.Cast<XmlNode>().FirstOrDefault(x => x.Name == "ARRAY-SIZE-SEMANTICS");
                    if (node != null) return node.InnerText; else return "";
                }
            }
        }
    
        class NodeCollections : IEnumerable<Node>, IEnumerator<Node>, IEnumerator
        {
            private XmlNode _base;
    
            private XmlNode _itl;
    
            public NodeCollections(XmlNode basenode)
            {
                _base = basenode;
                Reset();
            }
    
            public IEnumerator<Node> GetEnumerator()
            {
                return this;
            }
    
            IEnumerator System.Collections.IEnumerable.GetEnumerator()
            {
                return this;
            }
    
            public Node Current
            {
                get { return new Node(_itl); }
            }
    
            public void Dispose()
            {
                //throw new NotImplementedException();
            }
    
            object IEnumerator.Current
            {
                get { return Current; }
            }
    
            public bool MoveNext()
            {
                if (_base == null) return false;
                if (_itl == null && _base.FirstChild != null)
                {
                    _itl = _base.FirstChild;
                    return true;
                }
                if (_itl.NextSibling != null)
                {
                    _itl = _itl.NextSibling;
                    return true;
                }
                return false;
            }
    
            public void Reset()
            {
                _itl = null;
            }
        }
    
        class DGVItem
        {
            public string shortname { get; set; }
            public string category { get; set; }
            public string shortname1 { get; set; }
            public string category1 { get; set; }
            public string arraysize { get; set; }
            public string arraysizesemantic { get; set; }
            public string basetyperef { get; set; }
        }
    }
    
    
    评论
    解决 无用
    打赏 举报
  • threenewbee 2018-07-19 13:27
    评论
    解决 无用
    打赏 举报
  • threenewbee 2018-07-24 02:22

    大概看了下
    public NodeCollections Children
    {
    get
    {
    var sub = _base.ChildNodes.Cast().FirstOrDefault(x => x.Name == "SUB-ELEMENTS");
    if (sub == null)
    sub = _base.ChildNodes.Cast().FirstOrDefault(x => x.Name == "ELEMENTS");
    return new NodeCollections(sub);
    }
    }
    如果你要加载Tuner,这里要加上EVENTS
    if (sub == null)
    sub = _base.ChildNodes.Cast().FirstOrDefault(x => x.Name == "EVENTS");
    就写在Children的subelements后面

    评论
    解决 无用
    打赏 举报
  • 未知昵称 2018-07-24 02:33

    图片说明 @caozhy

    评论
    解决 无用
    打赏 举报
  • 未知昵称 2018-07-24 05:12

    图片说明

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题