1.新建XML文件:
<?xml version="1.0" encoding="utf-8" ?> <SystemInfo> <Class name="News" desc="文章栏目"> <Item name="NewsInfo" desc="新闻资讯">1</Item> <Item name="MediaCoverage" desc="媒体报道">2</Item> <Item name="Story" desc="爱心故事">3</Item> </Class> </SystemInfo>
2.新建一个类SystemInfo:
public class SystemInfo { public string Id { get; set; } public string Name { get; set; } public string Desc { get; set; } }
3.读取并将值存到SystemInfo中:
SystemInfo systemInfo = new SystemInfo(); List<SystemInfo> systemInfoList = new List<SystemInfo>(); XDocument doc = new XDocument(); doc = XDocument.Load("SystemInfo.xml"); var classData = (from n in doc.Root.Elements("Class") where n.Attribute("name").Value == "News" select n).ToList(); foreach (var item in classData.Elements("Item")) { systemInfo.Id = item.Value; systemInfo.Name = item.Attribute("name").Value; systemInfo.Desc = item.Attribute("desc").Value; systemInfoList.Add(systemInfo); }