简体   繁体   English

XML文件中的C#dataGridView

[英]C# dataGridView from XML file

I know there are lots of resources that show how to bind dataGridViews to XML files or populate from XML, but the XML i'm using as source is a bit more complex than any example I've seen used and I'm struggling. 我知道有很多资源可以显示如何将dataGridViews绑定到XML文件或从XML填充,但是我用作源的XML比我所看到的任何示例都复杂得多,而且我还在努力。 (although it really shouldn't be difficult) (尽管确实不难)

I basically need to run several queries (I think) to get the data I want to populate the DGV, because the elements I want the content from are on different parent nodes of the XML. 我基本上需要运行几个查询(我认为)以获取要填充DGV的数据,因为我想要内容的元素位于XML的不同父节点上。

Here is what I have, and the comments should show you what I'm trying to achieve: 这是我所拥有的,评论应向您显示我正在努力实现的目标:

XDocument xmlDoc = XDocument.Load("Techdocx_dml.xml");
                var q = from c in xmlDoc.Root.Descendants("dmentry")
                             .Descendants("avee")
                            //.Descendants("dmtitle") I also need to access this descendant
                    select new
                    {
                        modelic = c.Element("modelic").Value,
                        sdc = c.Element("sdc").Value,
                        chapnum = c.Element("chapnum").Value,
                        section = c.Element("section").Value,
                        subsect = c.Element("subsect").Value,
                        subject = c.Element("subject").Value,
                        discode = c.Element("discode").Value,
                        discodev = c.Element("discodev").Value,
                        incode = c.Element("incode").Value,
                        incodev = c.Element("incodev").Value,
                        itemloc = c.Element("itemloc").Value,

                       // techname = c.Element("techname").Value, 
                       //need this value, which is on the "dmtitle" node, not the "avee" node


                    };



                dataGridView1.DataSource = q.ToList();
                dataGridView1.ColumnHeadersVisible = false;

                dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;

This is what i want in my dataGridView: 这就是我想要的dataGridView中的内容:

AA A 32 3 5 00 01 A 018 A A | Some title 1 | Introduction 
AA A 32 3 5 00 01 A 920 A A | Some title 2 | Some infoname 2

How do I achieve this please? 请问我该如何实现? Example XML below: 下面的示例XML:

<dml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <dmentry>
        <addresdm>
            <dmc>
                <avee>
                    <modelic>AA</modelic>
                    <sdc>A</sdc>
                    <chapnum>32</chapnum>
                    <section>3</section>
                    <subsect>5</subsect>
                    <subject>00</subject>
                    <discode>01</discode>
                    <discodev>A</discodev>
                    <incode>018</incode>
                    <incodev>A</incodev>
                    <itemloc>A</itemloc>
                </avee>
            </dmc>
            <dmtitle>
                <techname>Some title 1</techname>
                <infoname>Introduction</infoname>
            </dmtitle>
            <issno issno="001" type="New"/>
            <issdate year="2016" month="06" day="10"/>
            <language language="SX" country="GB"/>
        </addresdm>
        <security class="1"/>
    </dmentry>
    <dmentry>
        <addresdm>
            <dmc>
                <avee>
                    <modelic>AA</modelic>
                    <sdc>A</sdc>
                    <chapnum>32</chapnum>
                    <section>3</section>
                    <subsect>5</subsect>
                    <subject>00</subject>
                    <discode>01</discode>
                    <discodev>A</discodev>
                    <incode>920</incode>
                    <incodev>A</incodev>
                    <itemloc>A</itemloc>
                </avee>
            </dmc>
            <dmtitle>
                <techname>Some title 2</techname>
                <infoname>Some infoname 2</infoname>
            </dmtitle>
            <issno issno="001" type="New"/>
            <issdate year="2016" month="06" day="10"/>
            <language language="SX" country="GB"/>
        </addresdm>
        <security class="1"/>
    </dmentry>
</dml>

Try this : 尝试这个 :

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.Xml.Linq;

namespace WindowsFormsApplication11
{
    public partial class Form1 : Form
    {
        const string FILENAME = @"c:\temp\test.xml";
        public Form1()
        {
            InitializeComponent();

            DataTable dt = new DataTable();
            dt.Columns.Add("modelic", typeof(string));
            dt.Columns.Add("sdc", typeof(string));
            dt.Columns.Add("chapnum", typeof(string));
            dt.Columns.Add("section", typeof(string));
            dt.Columns.Add("subsect", typeof(string));
            dt.Columns.Add("subject", typeof(string));
            dt.Columns.Add("discode", typeof(string));
            dt.Columns.Add("discodev", typeof(string));
            dt.Columns.Add("incode", typeof(string));
            dt.Columns.Add("incodev", typeof(string));
            dt.Columns.Add("itemloc", typeof(string));
            dt.Columns.Add("techname", typeof(string));
            dt.Columns.Add("infoname", typeof(string));


            XDocument doc = XDocument.Load(FILENAME);

            foreach (XElement addresdm in doc.Descendants().Where(x => x.Name.LocalName == "addresdm"))
            {
                XElement avee = addresdm.Descendants("avee").FirstOrDefault();
                XElement dmtitle = addresdm.Descendants("dmtitle").FirstOrDefault();
                dt.Rows.Add(new object[] {
                    (string)avee.Element("modelic"),
                    (string)avee.Element("sdc"),
                    (string)avee.Element("chapnum"),
                    (string)avee.Element("section"),
                    (string)avee.Element("subsect"),
                    (string)avee.Element("subject"),
                    (string)avee.Element("discode"),
                    (string)avee.Element("discodev"),
                    (string)avee.Element("incode"),
                    (string)avee.Element("incodev"),
                    (string)avee.Element("itemloc"),
                    (string)dmtitle.Element("techname"),
                    (string)dmtitle.Element("infoname")
                });

            }

            dataGridView1.DataSource = dt;


        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM