<?xml version="1.0" encoding="utf-8"?>
<Groups>
<Group ID="1">公司</Group>
<Group ID="4">个人</Group>
<Group ID="5">亲人</Group>
<Group ID="7">朋友</Group>
</Groups>怎么把这个XML里面的ID 与后面的名字做为键值存入Dictionary<string,string>里啊?比如("1","公司")这样的格式
解决方案 »
- 跪求怎么用。net统计access中的数据中条数
- vs2008 怎么把显示的字给改了
- 【接龙】每人进来写一个和C#有关的技术。
- 请教,Windows Service程序如何与桌面程序交互?
- 如何在listview中添加图片(图片下方显示文字)?很急哦,麻烦大家了哈
- 关于windowsService的问题!请各位大哥帮忙!用c#还不到两个星期~!
- ConfigurationSettings.AppSettings的问题。自己写的config总是一运行就消失~
- 请问.net的大小写哪些有影响,哪些没影响的呢?
- 如何检索字符串
- 已知完全的文件名,求如何分解该文件名,以得到文件名,文件扩展名,目录等资料。
- 初学者在线请教一个问题
- C#如何调用C++的函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;namespace XmlDoc
{
class Program
{
static void Main(string[] args)
{
string xmlFileName = @"D:\Fillex\VS2008Projects\CSDNAnswer\XmlDoc\XmlDoc\data.xml";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(xmlFileName); Dictionary<string, string> dict = new Dictionary<string, string>();
foreach (XmlNode node in xmlDoc.SelectNodes("//Group"))
{
dict.Add(node.Attributes["ID"].Value, node.InnerText);
} foreach (var keyvalue in dict)
Console.WriteLine("{0}:{1}", keyvalue.Key, keyvalue.Value); Console.ReadLine();
}
}
}
<Group ID=""1"">公司</Group>
<Group ID=""4"">个人</Group>
<Group ID=""5"">亲人</Group>
<Group ID=""7"">朋友</Group>
</Groups>"; System.Collections.Generic.Dictionary<string, string> _list = new Dictionary<string, string>();
foreach (var v in XDocument.Parse(xml).Descendants("Group"))
{
_list.Add(v.Attribute("ID").Value,v.Value);
}
XElement xe = XElement.Load("../../XMLFile1.xml");
Dictionary<int, string> dic = xe.Descendants("Group").ToDictionary(s => int.Parse(s.Attribute("ID").Value), v => v.Value);