2017-10-16 115 views
-2

我有一个简单的XML,使用XML TO LINQ分析,但是“代码”,“预算”数据不能以表格方式,试过很多方法,一直无法有效解决,问老师是否可以帮助解决问题,谢谢你的帮助。XML TO LINQ解析

主程序:

using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Xml.Linq; 

namespace test_1015_empl 
{ 
    public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     LinqExamples linq = new LinqExamples(); 
     linq.GetEmployeeList(); 
     dataGridView1.DataSource = linq.GetEmployeeList(); 
    } 

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) 
    { 
    } 
} 

}

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Xml.Linq; 

namespace test_1015_empl 
{ 
    internal class set 
    { 
    } 

public class Employee 
{ 
    public int EmployeeID { get; set; } 
    public string EmployeeName { get; set; } 
    public string EmployeePosition { get; set; } 
    public string EmployeeCountry { get; set; } 
    public Project[] Projects { get; set; } 
} 

public class Project 
{ 
    public string ProjectCode { get; set; } 
    public int ProjectBudget { get; set; } 
} 

public class LinqExamples 
{ 
    private List<Employee> employeeList; 

    public List<Employee> GetEmployeeList() 
    { 
     if (employeeList == null) 
      LoadXML(); 
     return employeeList; 
    } 

    private void LoadXML() 
    { 
     employeeList = 
      (
       from e in XDocument.Load(@"..\..\Employees.xml").Root.Elements("employee") 
       select new Employee 
       { 
        EmployeeID = (int)e.Element("id"), 
        EmployeeName = (string)e.Element("name"), 
        EmployeePosition = (string)e.Element("position"), 
        EmployeeCountry = (string)e.Element("country"), 
        Projects = 
        (
         from p in e.Elements("projects").Elements("project") 
         select new Project 
         { 
          ProjectCode = (string)p.Element("code"), 
          ProjectBudget = (int)p.Element("budget") 
         }).ToArray() 
       }).ToList(); 
    } 
} 

}

XML:

<?xml version="1.0"?> 
<employees> 
    <employee> 
    <id>1001</id> 
    <name>John</name> 
    <position>Developer</position> 
    <country>USA</country> 
    <projects> 
     <project> 
      <code>Orlando</code> 
      <budget>1000</budget> 
     </project> 
     <project> 
      <code>Rocket</code> 
      <budget>7000</budget> 
     </project> 
    </projects> 
</employee> 

<employee> 
    <id>1002</id> 
    <name>Tomas</name> 
    <position>QA</position> 
<country>USA</country> 
    <projects> 
     <project> 
      <code>Orlando</code> 
      <budget>1000</budget> 
     </project> 
     <project> 
      <code>Newman</code> 
      <budget>900</budget> 
     </project> 
    </projects> 
</employee> 

<employee> 
    <id>1003</id> 
    <name>Marina</name> 
    <position>QA</position> 
<country>Russia</country> 
    <projects> 
     <project> 
      <code>Orlando</code> 
      <budget>1000</budget> 
     </project> 
     <project> 
      <code>Newman</code> 
      <budget>900</budget> 
     </project> 
     <project> 
      <code>Rocket</code> 
      <budget>7000</budget> 
     </project> 
    </projects> 
</employee> 

<employee> 
    <id>1004</id> 
    <name>Suzan</name> 
    <position>Developer</position> 
<country>England</country> 
    <projects>   
     <project> 
      <code>Rocket</code> 
      <budget>7000</budget> 
     </project> 
    </projects> 
</employee> 

<employee> 
<id>1005</id> 
<name>Lucas</name> 
<position>IT</position> 
<country>England</country> 
<projects> 
    <project> 
    <code>Orlando</code> 
    <budget>1000</budget> 
    </project> 
    <project> 
     <code>Rocket</code> 
     <budget>7000</budget> 
    </project> 
    <project> 
     <code>Newman</code> 
    <budget>900</budget> 
    </project> 
    </projects> 
</employee> 
</employees> 

enter image description here

输出结果,减少“代码”,“预算”数据,如何使用分辨率?

+1

其中有三个项目。你期望输出是什么? – Enigmativity

+1

除了@ Enigmativity的问题之外,很高兴看到datagridview的xaml代码,因为问题很可能不是您的XML到LINQ解析,而是您尝试显示数据的方式。 –

+0

老师好: 我想能够输出的所有数据,如图所示: https://drive.google.com/file/d/0B6BlJrfBeE4pZ0o0ODFCaEVxTUE/view?usp=sharing –

回答

0

您可以使用两个DataGridView以及数据绑定作为主/明细。

var employeesBindingSource = new BindingSource(); 
employeesBindingSource.DataSource = employeeList; 

var projectsBindingSource = new BindingSource(); 
projectsBindingSource.DataSource = employeesBindingSource; 
projectsBindingSource.DataMember = nameof(Employee.Projects); 

employeesDataGridView.DataSource = employeesBindingSource; 
projectsDataGridView.DataSource = projectsBindingSource; 

见全样本:

你的员工
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Windows.Forms; 
using System.Xml.Linq; 

namespace WindowsFormsApp1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      //InitializeComponent(); 
      Size = new Size(600, 400); 

      var employeeList = 
      (
       from e in XDocument.Load("test.xml").Root.Elements("employee") 
       select new Employee 
       { 
        EmployeeID = (int)e.Element("id"), 
        EmployeeName = (string)e.Element("name"), 
        EmployeePosition = (string)e.Element("position"), 
        EmployeeCountry = (string)e.Element("country"), 
        Projects = 
        (
         from p in e.Elements("projects").Elements("project") 
         select new Project 
         { 
          ProjectCode = (string)p.Element("code"), 
          ProjectBudget = (int)p.Element("budget") 
         }).ToArray() 
       }).ToList(); 

      var employeesDataGridView = new DataGridView { Parent = this, Dock = DockStyle.Top }; 
      var projectsDataGridView = new DataGridView { Parent = this, Dock = DockStyle.Bottom }; 

      var employeesBindingSource = new BindingSource(); 
      employeesBindingSource.DataSource = employeeList; 

      var projectsBindingSource = new BindingSource(); 
      projectsBindingSource.DataSource = employeesBindingSource; 
      projectsBindingSource.DataMember = nameof(Employee.Projects); 

      employeesDataGridView.DataSource = employeesBindingSource; 
      projectsDataGridView.DataSource = projectsBindingSource; 
     } 
    } 

    public class Employee 
    { 
     public int EmployeeID { get; set; } 
     public string EmployeeName { get; set; } 
     public string EmployeePosition { get; set; } 
     public string EmployeeCountry { get; set; } 
     public Project[] Projects { get; set; } 
    } 

    public class Project 
    { 
     public string ProjectCode { get; set; } 
     public int ProjectBudget { get; set; } 
    } 
} 
+0

谢谢你的帮助 也许我能够认真的代码内容?不知道该怎么做才能正确 对不起,我是初学者,我试图弄清楚你写的方式 –

+0

@叶孟峰 - 查看更新 –

+0

非常感谢,我试着解析代码 谢谢对你的帮助 –