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文件进行插入。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下