惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 叶小钗
Last Week in AI
Last Week in AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
J
Java Code Geeks
The GitHub Blog
The GitHub Blog
博客园_首页
U
Unit 42
人人都是产品经理
人人都是产品经理
Engineering at Meta
Engineering at Meta
IT之家
IT之家
G
Google Developers Blog
L
LangChain Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
爱范儿
爱范儿
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
Martin Fowler
Martin Fowler
Jina AI
Jina AI
有赞技术团队
有赞技术团队
Apple Machine Learning Research
Apple Machine Learning Research
Vercel News
Vercel News
小众软件
小众软件
H
Help Net Security

博客园 - 阳光追梦

Xamarin Android ListView 控件使用 Xamarin Android -创建Splash Screen (一) Web Api 2 接口API文档美化 高仿Windows Phone QQ登录界面 Windows Phone 8 显示当前项目的使用内存,最大峰值,最大内存上限 Windows Phone 8 下载文件进度 Windows Phone 8 开发资料 jquery 选择器大全 转载:SQL 递归树 子父节点相互查询 Silverlight 代水印的查询文本框 Style Create a Settings Page for Windows phone Android 4.1 新增UI模板 Winwdos Phone 从手机中读取相册图片 windows phone 7 学习笔记 五 TileSample windows phone 学习笔记 四 Windows Phone 7 学习笔记三------文本框输入作用域 2011 西安世界园艺博览会-----Silverlight 电子地图上线 Esri for Window Phone 7(一) 加载BingMap Windows Phone 7 学习笔记------常用工具(二)
Linq 读取简单的XML数据
阳光追梦 · 2011-05-03 · via 博客园 - 阳光追梦

在项目开发中常用到这些,记录下来,以后忘记了,来查询

public class XmlCountryRepository : ICountryRepository
    {
        private static List<CountryData> countryList = null;

        static XmlCountryRepository()
        {
            XDocument loadedData = XDocument.Load("CountriesXML.xml");

            var data = from query in loadedData.Descendants("Country")
              select new CountryData
              {
                  Name = (string)query.Element("Name"),
                  Flag = (string)query.Element("Flag"),
                  Description = (string)query.Element("Description"),
                  Capital = (string)query.Element("Capital"),
                  ID = (int)query.Element("ID"),
              };
            countryList = data.ToList();
        }

        public IList<CountryData> GetCountryList()
        {
            return countryList;
        }

        public CountryData GetCountryById(int id)
        {
            return countryList.FirstOrDefault(c => c.ID == id);
        }
    }
<Countries>
  <Country>
    <Name>Germany</Name>
    <Flag>../Images/Germany.png</Flag>
    <ID>1</ID>
    <Description>Germany Description</Description>
    <Capital>Berlin</Capital>
  </Country>
  <Country>
    <Name>Grece</Name>
    <Flag>../Images/Greece.png</Flag>
    <ID>2</ID>
    <Description>Grece Description</Description>
    <Capital>Athens</Capital>
  </Country>
  <Country>
    <Name>France</Name>
    <Flag>../Images/France.png</Flag>
    <ID>3</ID>
    <Description>France Description</Description>
    <Capital>Paris</Capital>
  </Country>
  <Country>
    <Name>Italy</Name>
    <Flag>../Images/Italy.png</Flag>
    <ID>4</ID>
    <Description>Italy Description</Description>
    <Capital>Rome</Capital>
  </Country>
  <Country>
    <Name>Spain</Name>
    <Flag>../Images/Spain.png</Flag>
    <ID>5</ID>
    <Description>Spain Description</Description>
    <Capital>Madrid</Capital>
  </Country>
  <Country>
    <Name>UK</Name>
    <Flag>../Images/UK.png</Flag>
    <ID>6</ID>
    <Description>UK Description</Description>
    <Capital>London</Capital>
  </Country>
</Countries>