我有一個簡單的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>
輸出結果,減少「代碼」,「預算」數據,如何使用分辨率?
其中有三個項目。你期望輸出是什麼? – Enigmativity
除了@ Enigmativity的問題之外,很高興看到datagridview的xaml代碼,因爲問題很可能不是您的XML到LINQ解析,而是您嘗試顯示數據的方式。 –
老師好: 我想能夠輸出的所有數據,如圖所示: https://drive.google.com/file/d/0B6BlJrfBeE4pZ0o0ODFCaEVxTUE/view?usp=sharing –