文章出處
文章列表
上篇文章讀了如何通過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文件進行插入。
文章列表
全站熱搜