[英]Parsing XML with LINQ in C#
我正在嘗試使用LINQ在C#和XML之間進行映射。 當前,我有一個用C#定義的類,如下所示:
Item.cs
public class Item
{
public string DepartmentName { get; set; }
public Dictionary<string, string> DepartmentNames { get; set; }
public string Name { get; set; }
public Dictionary<string, string> Names { get; set; }
}
我的XML文件如下所示:
departments.xml
<Departments>
<Department Name="Sports" Title="Sports Department" Path="/sports" en-us="Sports" de-de="Sport">
<Item Name="Football" en-us="Football" de-de="Fußball" es-mx="" />
<Item Name="TennisBall" en-us="Tennis Ball" de-de="Tennisball" />
</Department>
<Department Name="Automotive" Title="Automotive Department" Path="/autos" en-us="Automotive" de-de="kraftfahrtechnisch">
<Item Name="Oil" en-us="Oil" de-de="Öl" />
<Item Name="Tires" en-us="Tires" de-de="Bereifung" es-mx="Ruedas" />
</Department>
</Departments>
基本上,我有一些要加載到確定屬性中的值。 然后,我有一個我想加載到字典中的屬性列表(“ zh-cn”,“ de-de”,“ es-mx”)。 目前,我有以下內容:
var items = XDocument.Parse(File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"App_Data\Items.xml")))
.Element("Departments")
.Elements("Department")
.Elements("Item")
.Select(e => new Item
{
DepartmentName = ?,
DepartmentNames = ?,
Name = e.Attribute("Name").Value,
Names = ?
}).ToList();
我不確定如何從a)父元素b)加載Dictionary對象加載屬性。 LINQ甚至有可能嗎? 本質上,我試圖在內存中整理數據結構。
在這種情況下,需要在嵌套循環中訪問外部元素的屬性,我發現from / select
語法中的Linq更加舒適。 從而:
var doc = XDocument.Load(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "InputData.xml"));
var query = from department in doc.Element("Departments").Elements("Department")
from item in department.Elements("Item")
select new Item
{
DepartmentName = department.Attribute("Name").Value,
DepartmentNames = department.Attributes().Where(a => a.Name != "Name").ToDictionary(a => a.Name.LocalName, a => a.Value),
Name = item.Attribute("Name").Value,
Names = item.Attributes().Where(a => a.Name != "Name").ToDictionary(a => a.Name.LocalName, a => a.Value),
};
var items = query.ToList();
在這里,我假設您希望將字典中除Name
屬性(其具有自己的屬性)之外的每個屬性都放置在字典中。
更新
如果您有待放入字典的已知屬性列表,則可以執行以下操作:
var attributes = new HashSet<string>(new[] { "en-us", "de-de", "es-mx" }); // Possibly initialized in a static constructor.
var query = from department in doc.Element("Departments").Elements("Department")
from item in department.Elements("Item")
select new Item
{
DepartmentName = department.Attribute("Name").Value,
DepartmentNames = department.Attributes().Where(a => attributes.Contains(a.Name.LocalName)).ToDictionary(a => a.Name.LocalName, a => a.Value),
Name = item.Attribute("Name").Value,
Names = item.Attributes().Where(a => attributes.Contains(a.Name.LocalName)).ToDictionary(a => a.Name.LocalName, a => a.Value),
};
var items = query.ToList();
我認為下面的代碼是您想要的。 我在這里進行了測試,它確實有效,但是我不確定這是否是您的意圖。 我只是用所有語言值填充字典。
var items = XDocument.Parse(
File.ReadAllText(
Path.Combine(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
"InputData.xml")))
.Element("Departments")
.Elements("Department")
.Select(
d => d.Elements("Item").Select(e => new Item
{
DepartmentName = d.Attribute("Name").Value,
DepartmentNames = new Dictionary<string,string>()
{
{ "en-us", d.Attribute("en-us").Value },
{ "de-de", d.Attribute("de-de").Value}
},
Name = e.Attribute("Name").Value,
Names = new Dictionary<string,string>()
{
{ "en-us", e.Attribute("en-us").Value},
{ "de-de", e.Attribute("de-de").Value}
}
})).ToList();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.