2016-10-06 7 views
0

Ich weiß, es gibt viele Ressourcen, die zeigen, wie DataGridViews zu XML-Dateien binden oder von XML auffüllen, aber das XML, das ich als Quelle verwende, ist ein bisschen komplexer als jedes Beispiel, das ich gesehen habe benutzt und ich kämpfe. (obwohl es wirklich nicht schwierig sein sollte)C# dataGridView von XML-Datei

Ich muss im Grunde mehrere Abfragen ausführen (denke ich), um die Daten zu erhalten, die ich die DGV füllen möchte, da die Elemente, die ich möchte, den Inhalt von verschiedenen übergeordneten Knoten sind das XML. Hier

ist, was ich habe, und die Kommentare sollten Sie zeigen, was ich zu erreichen bin versucht:

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; 

Dies ist, was ich in meinem Datagridview wollen:

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 

Wie kann ich erreiche das bitte? Beispiel XML unter:

<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> 
+0

Könnten Sie bitte ein Beispiel für die Ausgabe zur Verfügung stellen? – iceDragon

+0

Ich bekomme genau das, was Sie von meiner Abfrage erwarten würden. Was ich möchte ist das gleiche, aber mit dem Zusatz der Werte in der Abfrage, die ich auskommentiert habe. Also die Werte aller Kindelemente im Tag (was ich bekomme), aber auch der Wert der beiden Kindelemente von . – Daedalus

+0

Sie könnten eine DataTable mit allen gewünschten Spalten erstellen und dann jede Zeile mit zwei Linq-Abfragen füllen. oder erstellen Sie einfach eine Klasse mit allen Eigenschaften und weisen Sie diese Eigenschaften in zwei Abfragen zu – iceDragon

Antwort

1

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; 


     } 
    } 
} 
+0

Legende. Vielen Dank! – Daedalus