簡體   English   中英

使用linq to XML將xml文件導入datagridview。 數據無法正確顯示

[英]Importing xml file to datagridview using linq to XML. data not displaying correctly

我的XML檔案...

<?xml version="1.0" encoding="UTF-8"?>
<files>
        <file type="main">
<document>Report.pdf</document>
         <field name="Company">Northwind</field>
         <field name="Description">monthly report</field>
         <line>
               <field name="Description">Error</field>
               <field name="Type">4444</field>
         </line>
         <line>
               <field name="Description">Info</field>
               <field name="Type">4562</field>
         </line>
         <line>
               <field name="Description">Error</field>
               <field name="Type">2135</field>
         </line>
          <field name="Analyst">Bob</field>
          <field name="Manager">Steve</field>
          <field name="Dept">Finance</field>
          </file>
</files>

目前的我的代碼...

XElement xdoc = XElement.Load(@"C:\xmltest\input.xml");

                var lines = from item in xdoc.Descendants("line")

                            select new
                            {
                                Description = item.Value,
                                Type = item.Value

                            };

                dataGridView1.DataSource = lines.ToArray();

這些是我得到的結果...

在此處輸入圖片說明

我想要的結果是...

在此處輸入圖片說明

我認為該代碼可能有效...

 XElement xdoc = XElement.Load(@"C:\xmltest\input.xml");

                var lines = from item in xdoc.Descendants("line")

                            select new
                            {
                                Description = item.Attribute("field").Value,
                                Type = item.Value

                            };

                dataGridView1.DataSource = lines.ToArray();

我收到的錯誤是...

“你調用的對象是空的。”

您可以嘗試按屬性過濾,如下所示:

XElement xdoc = XElement.Load(@"XMLFile1.xml");

var lines = from item in xdoc.Descendants("line")
            select new
            {
                Description = item.Elements("field").Where(e => (string)e.Attribute("name") == "Description").First().Value,
                Type = item.Elements("field").Where(e => (string)e.Attribute("name") == "Type").First().Value
            };

var array = lines.ToArray();

foreach (var item in array)
{
    Console.WriteLine($"{item.Description}\t{item.Type}");
}

它將產生以下結果:

Error   4444
Info    4562
Error   2135   

數據集怎么樣?

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;

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

            DataSet ds = new DataSet();
            ds.ReadXml(FILENAME);

            dataGridView1.DataSource = ds.Tables[1];
        }
    }
}
​

您的代碼存在的問題是:

item.Attribute("field").Value  //this line

只需看看您的第一個查詢,它就會返回該結果,因為item代表每個line節點,其中包含相應的field節點。 假設第一item是:

<line>
  <field name="Description">Error</field>
  <field name="Type">4444</field>
</line>

等等...

現在,在第二個查詢中,當您說出item.Attribute("field").Value ,它將拋出Null reference exception因為每個item都不包含屬性field (如您在上面看到的),而是一個element 因此,您應該改寫item.Element("field") 但是,由於您要基於屬性值Descriptionname來獲取數據,因此仍然無法獲得預期的結果。 您可以這樣編寫查詢:

var lines = from item in xdoc.Descendants("line")
            let fields = item.Elements("field")
            select new
                {
                   Description = (string)fields
                       .FirstOrDefault(n => (string)n.Attribute("name") == "Description"),
                   Type = (string)fields
                       .FirstOrDefault(n => (string)n.Attribute("name") == "Type"),
                };

說明:

xdoc.Descendants("line")將如上所述獲取所有行節點,現在在其中需要查找所有fields節點,以便將其存儲在一個名為fields的變量中。 最后,在投影時,我已經使用FirstOrDefault方法來獲取第一個匹配的name屬性,其值是DescriptionType並獲取它的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM