首页 > 其他 > 详细

LinqToXML~读XML文件续

时间:2014-01-27 18:19:53      阅读:337      评论:0      收藏:0      [点我收藏+]

上篇文章读了如何通过linq to xml去读取XML文件,而这讲主要通过linq to xml来读取由属性组件的XML文件,例如读取一个web.config的XML格式的配置文件,下面是config文件的部分内容:

bubuko.com,布布扣
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
</configuration>
bubuko.com,布布扣

下面是linq to xml的代码:

bubuko.com,布布扣
            #region LinqtoXML测试2
            System.Console.WriteLine("Loading XML data2...");
            var dataXml =
                (from e in XElement.Load("D:\\config.xml")
                                   .Element("configSections")
                                   .Elements("section")
                 select new
                     {
                         name = (string)e.Attribute("name"),
                         type = (string)e.Attribute("type"),
                     }).ToList();
            dataXml.ForEach(i => System.Console.WriteLine(i.name + "    " + i.type));
            #endregion
bubuko.com,布布扣

我们看一下结果:

bubuko.com,布布扣

基本上,对于XML文件的读取也就是两种类型了,下讲我们将会讲一下如何对两种XML文件进行插入。

LinqToXML~读XML文件续

原文:http://www.cnblogs.com/lori/p/3534909.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!