你可以使用下面的代码来获得所需要的词典:
StringBuilder s = new StringBuilder();
s.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
s.AppendLine("<Integration>");
s.AppendLine("<FiledMappings name =\"Employee\">");
s.AppendLine("<Field Name=\"EmployeeID\">");
s.AppendLine("<DataSource>EmployeeNO</DataSource>");
s.AppendLine("</Field>");
s.AppendLine("<Field Name=\"Department\">");
s.AppendLine("<DataSource>Department</DataSource>");
s.AppendLine("</Field>");
s.AppendLine("<Field Name=\"EmployeeName\">");
s.AppendLine("<DataSource>Name</DataSource>");
s.AppendLine("</Field>");
s.AppendLine("</FiledMappings>");
s.AppendLine("</Integration>");
Dictionary<string, string> d = new Dictionary<string, string>();
XmlDocument doc = new XmlDocument();
doc.LoadXml(s.ToString());
XmlNode x = doc.ChildNodes[1].ChildNodes[0];
foreach (XmlNode n in x.ChildNodes)
d[n.Attributes[0].Value] = n.FirstChild.FirstChild.Value;
foreach (KeyValuePair<string, string> p in d)
Console.WriteLine(string.Format("{0}:\t{1}", p.Key, p.Value));
Console.ReadLine();
或者,如果它可能似乎可以使用.net 3.5,您可以利用Linq到XML,请参阅:
StringBuilder s = new StringBuilder();
s.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
s.AppendLine("<Integration>");
s.AppendLine("<FiledMappings name =\"Employee\">");
s.AppendLine("<Field Name=\"EmployeeID\">");
s.AppendLine("<DataSource>EmployeeNO</DataSource>");
s.AppendLine("</Field>");
s.AppendLine("<Field Name=\"Department\">");
s.AppendLine("<DataSource>Department</DataSource>");
s.AppendLine("</Field>");
s.AppendLine("<Field Name=\"EmployeeName\">");
s.AppendLine("<DataSource>Name</DataSource>");
s.AppendLine("</Field>");
s.AppendLine("</FiledMappings>");
s.AppendLine("</Integration>");
XElement x = XElement.Parse(s.ToString());
Dictionary<string, string> d = x.Element("FiledMappings").Elements("Field").ToDictionary(e => e.Attribute("Name").Value, e => e.Element("DataSource").Value);
foreach (KeyValuePair<string, string> p in d)
Console.WriteLine(string.Format("{0}:\t{1}", p.Key, p.Value));
Console.ReadLine();
如果你的O \ P个字典是这样的 '{ “雇员“:”“EmployeeNO”, “Department”:“Department”, ..... }'?? – 2012-07-30 02:27:30