我想将XML文件转换为字典。 XML文件的结构如下。
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Definitions>
<Organization ID="201">
<Department Name="Facility">
<Employees>200</Employees>
<Building>3</Building>
<Obj>classnameA</Obj>
</Department>
<Department Name="IT">
<Employees>12</Employees>
<Building>2</Building>
<Obj>classnameB</Obj>
</Department>
</Organization>
</Definitions>
我想使用嵌套字典,它可以被概念化为一个表格,以便&#34;部门&#34;是行号,&#34;组织&#34;是列号,&#34;员工&#34;,&#34;建筑&#34;和&#34; Obj&#34;是那里的价值观。因此,我创建了以下类(在this thread的帮助下):
public class NestedDictionary<K1, K2, V> :
Dictionary<K1, Dictionary<K2, V>> { }
然后,我需要通过遍历树来将我的XML文件的内容转换为 NestedDictionary 。如何添加三个属性&#34;员工&#34;,&#34;建设&#34;和&#34; Obj&#34;到我的字典?
private static void XmlToDict(XmlNode definitions)
{
NestedDictionary<int, string, string> dictionary = new NestedDictionary<int, string, string>();
foreach (XmlNode idNode in definitions)
{
foreach (XmlNode nameNode in idNode)
{
// The following line does not work
dictionary.Add(idNode.Name, nameNode.Name, nameNode);
}
}
}
答案 0 :(得分:1)
此LINQ表达式将您的XML数据转换为嵌套字典。它假设element
来自XDocument
命名空间的XElement
或System.Xml.Linq
类型。
var element = XElement.Parse(yourXML);
var result = element.Descendants("Organization")
.Select(org => new {
ID = org.Attribute("ID").Value,
Departments = org.Descendants("Department")
.Select(dept => new {
Name = dept.Attribute("Name").Value,
Employees = int.Parse(dept.Element("Employees").Value),
Building = dept.Element("Building").Value,
Obj = dept.Element("Obj").Value
})
})
.ToDictionary(
org => int.Parse(org.ID),
org => org.Departments.ToDictionary(dept => dept.Name));
结果成为<int, Dictionary<string, anonymous_type>>
的字典。当然,如果您不希望拥有匿名类型,您可以声明自己的Department
类:
public class Department
{
public int Building { get; set; }
public int Employees { get; set; }
public string Obj { get; set; }
}
并致电new Department { ... }
而不是上面显示的匿名new { ... }
。该表达式将改为生成类型为<int, Dictionary<string, Department>>
不使用LINQ :
public class XmlConverter
{
public static Dictionary<int, Dictionary<string, Department>> Convert(XmlDocument xdoc)
{
Dictionary<int, Dictionary<string, Department>> result = new Dictionary<int, Dictionary<string, Department>>();
foreach (XmlNode org in xdoc.SelectNodes("Definitions/Organization")) {
int orgId = int.Parse(org.SelectSingleNode("@ID").Value);
result.Add(orgId, GetDepartments(org));
}
return result;
}
private static Dictionary<string, Department> GetDepartments(XmlNode org)
{
Dictionary<string, Department> result = new Dictionary<string, Department>();
foreach (XmlNode dept in org.SelectNodes("Department")) {
string deptName = dept.SelectSingleNode("@Name").Value;
Department d = new Department();
d.Employees = int.Parse(dept.SelectSingleNode("Employees/text()").Value);
d.Building = int.Parse(dept.SelectSingleNode("Building/text()").Value);
d.Obj = dept.SelectSingleNode("Obj/text()").Value;
result.Add(deptName, d);
}
return result;
}
}
我们将组织表示为Dictionary<string, Department>
,其键是部门名称,值是部门对象。
整个树表示为Dictionary<int, Dictionary<string, Department>>
,其键是组织ID,值是部门词典。
答案 1 :(得分:0)
在Microsoft Visual Studio中将粘贴XML用作C#类。您可以使用生成的类来描述您的字典,元组或您的需求,属性/对象的名称将与您构建的Xml文件完全相同。 使用:
1. Visual Studio 2012 IDE you can convert XML document into C# classes as a Serializable type.
OR
2. In .NET framework 4.5 there you have: Menu -> Edit -> Paste Special -> Paste Xml as Classes.
此功能为我节省了很多工作,我在此之前使用过:
可以在[InstallDrive]:\ Program Files \ Microsoft Visual Studio .NET [2003] \ SDK [v1.1] \ bin目录中找到Xsd.exe。 该实用程序的源默认输出语言是C#生成XSD,并使用该XSD生成标准C#类,最后生成强类型数据集。
打开命令提示符并键入xsd.exe /?查看该实用程序的所有commad和switch选项。
它如何运作?
Locate the Command Prompt from your version of visual studio: 1. Check if xsd.exe exist (type xsd.exe) if true a list of commads will be displayed. 2. Navigate to your work folder. (cd, cd..) 3. Create your xml file MyFile.xml 4. Run the transformation from xml to xsd with xsd.exe MyFile.xml 5. Next create MyFile.cs wit command xsd.exe MyFile.xsd /c In the folder is now created a file .cs with the C# class ready to read from xml.