簡體   English   中英

XML文件中的C#dataGridView

[英]C# dataGridView from XML file

我知道有很多資源可以顯示如何將dataGridViews綁定到XML文件或從XML填充,但是我用作源的XML比我所看到的任何示例都復雜得多,而且我還在努力。 (盡管確實不難)

我基本上需要運行幾個查詢(我認為)以獲取要填充DGV的數據,因為我想要內容的元素位於XML的不同父節點上。

這是我所擁有的,評論應向您顯示我正在努力實現的目標:

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;

這就是我想要的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

請問我該如何實現? 下面的示例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>

嘗試這個 :

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