LinqToXML~读XML文件续

简介:

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

<?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>

下面是linq to xml的代码:

            #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

我们看一下结果:

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

本文转自博客园张占岭(仓储大叔)的博客,原文链接:LinqToXML~读XML文件续,如需转载请自行联系原博主。

目录
相关文章
|
14天前
|
XML Java 数据库连接
mybatis中在xml文件中通用查询结果列如何使用
mybatis中在xml文件中通用查询结果列如何使用
15 0
|
16天前
|
XML JavaScript 前端开发
xml文件使用及解析
xml文件使用及解析
|
1月前
|
XML C# 数据格式
使用C#操作XML文件
使用C#操作XML文件
11 0
|
1月前
|
Java
java实现遍历树形菜单方法——映射文件VoteTree.hbm.xml
java实现遍历树形菜单方法——映射文件VoteTree.hbm.xml
10 0
|
1月前
|
Kubernetes Cloud Native Java
Activiti 简介以及最新activiti依赖 pom.xml文件(使用时注意对应版本号)
Activiti 简介以及最新activiti依赖 pom.xml文件(使用时注意对应版本号)
39 1
|
1月前
|
XML Java 数据库连接
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——hibernate的config文件(hibernate.cfg.xml)
struts+hibernate+oracle+easyui实现lazyout组件的简单案例——hibernate的config文件(hibernate.cfg.xml)
11 0
|
2月前
|
XML JavaScript 数据格式
打开 XML 文件报错 There is no Unicode byte order mark
打开 XML 文件报错 There is no Unicode byte order mark
43 0
|
2月前
|
XML 存储 数据格式
什么是 XML 文件的 Schema
什么是 XML 文件的 Schema
30 0
|
2天前
|
XML 数据格式
小米备份descript.xml文件
小米备份descript.xml文件
11 0
|
5天前
|
XML C# 数据格式
C# 解析XML文件
C# 解析XML文件
14 1