您好我正在尝试使用 XElement 和 DynamicLinq 创建一个通用/动态类的列表,然后在 dataGridView1.DataSource 中显示
首先,让我们使用 XElement 类制作 xml。
// a List of String that are going to be our dynamic class's properties.
List<string> elements = new List<string>();
elements.Add("Name");
elements.Add("Address");
elements.Add("OtherElements1");
elements.Add("OtherElements2");
elements.Add("OtherElements3");
// XML Root Element
XElement XElement_Root = new XElement("Root");
// Item Element, This is our dynamic class
XElement XElement_Item = new XElement("Item");
// Insert the elements as XElements in the Item XElement
// This is like defining our Properties to our Item Class
foreach (String element in elements)
{
XElement_Item.Add(new XElement(element));
}
// Add 5 "Item" classes to the List "Root"
for (int cc = 0; cc < 5; cc++ )
{
foreach (String elementName in elements)
{
// Adding a Random 10 length string to each "Item" Element(property)
XElement_Item.Element(elementName).Value = RandomString(10);
}
// Adding the populated "Class"(Item) to the "List<>"(Root)
XElement_Root.Add(new XElement(XElement_Item));
}
// Preview of the created xml.
String xml = XElement_Root.ToString();
它应该看起来像这样。
- Root -
- Item -
- Name - KJHLRGOUDM - /Name -
- Address - QEOARCIIHO - /Address -
- OtherElements1 - LYHNXEPZCU - /OtherElements1 -
- OtherElements2 - MNSHTNYVXY - /OtherElements2 -
- OtherElements3 - DETZKZPJCE - /OtherElements3 -
- /Item -
- Item -
- Name - HYCNPMBTON - /Name -
- Address - QOSIADMHGE - /Address -
- OtherElements1 - ENLIKGEICX - /OtherElements1 -
- OtherElements2 - OGXYNOKFRH - /OtherElements2 -
- OtherElements3 - LEGJIPMKZH - /OtherElements3 -
- /Item -
...
- /Root -
这一切都很好。但现在我解决了我的问题,我想将此 xml 转换为类“项目”列表以添加到我的 dataGridView1.DataSource;
这有效,但它不是通用/动态的。
// Normal Linq !!! NOT GENERIC / DYNAMIC !!!
var listOfClassItem =
from classItem in XElement_Root.Descendants("Item") // "Item" is our class Name
select new
{
Name = classItem.Element("Name").Value,
Address = classItem.Element("Address").Value + " " +
classItem.Element("OtherElements1").Value,
PropertyZ = classItem.Element("OtherElements3").Value
};
// populate the dataGridView1.DataSource with the New List of Class Item
dataGridView1.DataSource = listOfClassItem.ToList();
网格然后看起来像这样。对不起,没有图像,工作块everyting :(
=============================================================
| Name | Address | PropertyZ |
=============================================================
| SCTGCITLTN | NKMGDIYTGP LUCGNJCUNQ | DYJJJGPZDH |
| KBMZTYAMTS | FZXDYVFAJO KVIMMNKSWG | OLKTODAGLO |
| APQPXOSANG | NHQMYCIRWL QBHKZYKPXI | UERMLHVXVL |
| UIJZXZIFPY | DDCESJZHHT PHHERLJUZS | WHMKNQCMUB |
| INPBWNBEIM | QNGXVQKQRO NRXBXIUWRB | DQAYPIBOPX |
=============================================================
好的,所以这很好,但不是通用/动态的,然后我尝试的是这个。
// Dynamic Linq now this should be more GENERIC / DYNAMIC
// I want the User to be able to select which fields (properties) they want to show in the DataGrid
String selectQuery = GetSelectedFields(); // private String GetSelectedFields() { return ...}
// selectQuery will then be some thing like.
selectQuery = " new ( Element(\"Name\").Value as Name, " +
" Element(\"Address\").Value + \" \" + Element(\"OtherElements1\").Value as Address, " +
" Element(\"OtherElements3\").Value as PropertyZ )";
// Then using Dynamic Linq to get the generic / dynamic class from the selection.
// !!! THIS IS WHERE I'M STUCK !!! this does not work. :(
var listOfClassItem2 = XElement_Root.Descendants("Item").AsQueryable().Select(selectQuery);
// I Found that this was the easiest way to get the "var" back to a List, to add to the .DataSource
// because the IQueryable above does not have a .ToList();
List<object> ListOfItem = new List<object>();
foreach (var varItem in listOfClassItem2)
{
ListOfItem.Add(varItem);
}
// populate the dataGridView1.DataSource with the New List of Class Dynamic Item
dataGridView1.DataSource = ListOfItem;
我收到以下错误消息。
No applicable method 'Element' exists in type 'XElement'
有人可以帮我吗,
XElement_Root.Descendants("Item").AsQueryable().Select(selectQuery);
这样我就可以获得与使用 Normal Linq 时相同的结果。谢谢。