我想将XML文件转换为Dictionary。 XML文件的结构如下。
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Definitions>
<Organization ID="416">
<Department Name="Facility">
<Destination Employees="Something" Obj="someObject" Building="none" Type="Int16"\>
</Department>
</Organization>
<Organization ID="502">
<Department Name="IT">
<Destination Employees="Something" Obj="someObject" Building="none" Type="Int16"\>
</Department>
<Department Name="Specialty">
<Destination Employees="Something" Obj="someObject" Building="none" Type="Int16"\>
<Destination Employees="SomethingElse" Obj="someObject" Building="none" Type="Int16"\>
</Department>
</Organization>
</Definitions>
我想使用嵌套字典,可以将其概念化为表格,使“部门”为行号,“组织”为列号,“员工”,“对象”,“建筑物” ,“类型”是在那里找到的值。因此,我创建了以下类:
// Stores constant data of each tuple <Organization,Department>
public class DatSet
{
public string Employees { get; set; }
public string Obj { get; set; }
public string Building { get; set; }
public string Type { get; set; }
}
和以下字典来存储数据:
// Nested Diectionary to allow fast access to XML content
public static Dictionary<int, Dictionary<string, IEnumerable<DatSet>>>;
整个树表示为Dictionary<int, Dictionary<string, DatSet>>
,其键是组织ID,而值又是字典,其键是部门名称,值是数据集。
使用以下LINQ表达式,我想将XML数据转换为嵌套字典。但是,我收到一条错误消息,指出 lambda-expression 无法转换为类型 System.Collections.Generic.IEqualityComparer (在org => org.DatSet.ToDictionary(dep => dep.Name));
中)。
// Converts XML File to nested Dictionary
private static Dictionary<int, Dictionary<string, IEnumerable<DatSet>>> XmlToDict(XDocument element)
{
return element.Descendants("Organization")
.Select(org => new
{
ID = org.Attribute("ID").Value,
dep = org.Descendants("Department")
.Select(dep => new
{
Name = dep.Attribute("Name").Value,
DatSet = dep.Descendants("Destination")
.Select(dest => new DatSet
{
Employees = dest.Attribute("Employees").Value,
Obj = dest.Attribute("Obj").Value,
Building = dest.Attribute("Building").Value,
Type = dest.Attribute("Type").Value,
})
})
})
.ToDictionary(
org => int.Parse(org.ID),
org => org.DatSet.ToDictionary(dep => dep.Name));
}
修改
当我按照ChrFin的建议更改最后一行时,错误消失。但是,我没有得到以下XMLException:
XmlException: Unexpected token. Name is required here. file:///C:/Data/projekt_2/Assets/Resources/DataDef.xml Line 6, position 72.
Mono.Xml2.XmlTextReader.ReadAttributes (Boolean isXmlDecl)
Mono.Xml2.XmlTextReader.ReadStartTag ()
Mono.Xml2.XmlTextReader.ReadContent ()
Mono.Xml2.XmlTextReader.Read ()
System.Xml.XmlTextReader.Read ()
Mono.Xml.XmlFilterReader.Read ()
Mono.Xml.XmlFilterReader.Read ()
System.Xml.Linq.XElement.LoadCore (System.Xml.XmlReader r, LoadOptions options)
System.Xml.Linq.XNode.ReadFrom (System.Xml.XmlReader r, LoadOptions options)
System.Xml.Linq.XContainer.ReadContentFrom (System.Xml.XmlReader reader, LoadOptions options)
System.Xml.Linq.XElement.LoadCore (System.Xml.XmlReader r, LoadOptions options)
System.Xml.Linq.XNode.ReadFrom (System.Xml.XmlReader r, LoadOptions options)
System.Xml.Linq.XContainer.ReadContentFrom (System.Xml.XmlReader reader, LoadOptions options)
System.Xml.Linq.XElement.LoadCore (System.Xml.XmlReader r, LoadOptions options)
System.Xml.Linq.XNode.ReadFrom (System.Xml.XmlReader r, LoadOptions options)
System.Xml.Linq.XContainer.ReadContentFrom (System.Xml.XmlReader reader, LoadOptions options)
System.Xml.Linq.XDocument.ReadContent (System.Xml.XmlReader reader, LoadOptions options)
System.Xml.Linq.XDocument.LoadCore (System.Xml.XmlReader reader, LoadOptions options)
System.Xml.Linq.XDocument.Load (System.String uri, LoadOptions options)
System.Xml.Linq.XDocument.Load (System.String uri)
Input.Awake () (at Assets/Scripts/Input/Input.cs:41)
答案 0 :(得分:1)