LINQ to XML的文档类型有哪些
发表于:2026-11-04 作者:千家信息网编辑
千家信息网最后更新 2026年11月04日,这篇文章主要讲解了"LINQ to XML的文档类型有哪些",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"LINQ to XML的文档类型有哪些"吧!L
千家信息网最后更新 2026年11月04日LINQ to XML的文档类型有哪些
这篇文章主要讲解了"LINQ to XML的文档类型有哪些",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"LINQ to XML的文档类型有哪些"吧!
LINQ to XML可以看作是一个 "better DOM" 编程模型,可以和 System.Xml.dll 程序集中的很多成员交互。
一、命名空间
System.Xml.Linq.dll 程序集定义了三个命名空间:System.Xml.Linq, System.Xml.Schema 和 System.Xml.XPath
最核心的是 System.Xml.Linq, 定义了对应 XML 文档个方面的很多类型
定义XML文档类型
二、编程方式创建XML文档
以前的 .NET XML编程模型需要使用很多冗长的 DOM API,而 LINQ to XML 则完全可以用与 DOM 无关的方式与 XML 文档交互。这样不但大大减少了代码行,而且这种编程模型可以直接映射到格式良好的XML文档结构。
static void CreateFunctionalXmlElement() { // A "functional" approach to build an // XML element in memory. XElement inventory = new XElement("Inventory", new XElement("Car", new XAttribute("ID", "1"), new XElement("Color", "Green"), new XElement("Make", "BMW"), new XElement("PetName", "Stan") ) ); // Call ToString() on our XElement. Console.WriteLine(inventory); }在内存中创建LINQ to XML文档
static void CreateFunctionalXmlDoc( { XDocument inventoryDoc = new XDocument( new XDeclaration("1.0", "utf-8", "yes"), new XComment("Current Inventory of AutoLot"), new XElement("Inventory", new XElement("Car", new XAttribute("ID", "1"), new XElement("Color", "Green"), new XElement("Make", "BMW"), new XElement("PetName", "Stan") ), new XElement("Car", new XAttribute("ID", "2"), new XElement("Color", "Pink"), new XElement("Make", "Yugo"), new XElement("PetName", "Melvin") ) ) ); // Display the document and save to disk. Console.WriteLine(inventoryDoc); inventoryDoc.Save("SimpleInventory.xml"); }三、使用LINQ查询创建XML文档
static void CreateXmlDocFromArray() { // Create an anonymous array of anonymous types. var data = new [] { new { PetName = "Melvin", ID = 10 }, new { PetName = "Pat", ID = 11 }, new { PetName = "Danny", ID = 12 }, new { PetName = "Clunker", ID = 13 } }; // Now enumerate over the array to build // an XElement. XElement vehicles = new XElement("Inventory", from c in data select new XElement("Car", new XAttribute("ID", c.ID), new XElement("PetName", c.PetName) ) ); Console.WriteLine(vehicles); }四、加载和解析LINQ to XML内容
static void LoadExistingXml() { // Build an XElement from string. string myElement = @"'3'> Yellow Yugo "; XElement newElement = XElement.Parse(myElement); Console.WriteLine(newElement); Console.WriteLine(); // Load the SimpleInventory.xml file. XDocument myDoc = XDocument.Load("SimpleInventory.xml"); Console.WriteLine(myDoc); } 五、遍历内存中的LINQ to XML 文档
LINQ to XML 示例:
"0"> Ford Blue Chuck "1"> VW Silver Mary "2"> Yugo Pink Gipper "55"> Ford Yellow 862 CHAPTER 24 n PROGRAMMING WITH THE LINQ APIS Max "98"> BMW Black Zippy
LINQ to XML 加载
static void Main(string[] args) { Console.WriteLine("***** Fun with LINQ to XML *****\n"); // Load the Inventory.xml document into memory. XElement doc = XElement.Load("Inventory.xml"); // We will author each of these next PrintAllPetNames(doc); Console.WriteLine(); GetAllFords(doc); Console.ReadLine(); }LINQ to XML遍历
static void PrintAllPetNames(XElement doc) { var petNames = from pn in doc.Descendants("PetName") select pn.Value; foreach (var name in petNames) Console.WriteLine("Name: {0}", name); }LINQ to XML查询
static void GetAllFords(XElement doc) { var fords = from c in doc.Descendants("Make") where c.Value == "Ford" select c; foreach (var f in fords) Console.WriteLine("Name: {0}", f); }六、修改LINQ to XML 文档
static void AddNewElements(XElement doc) { // Add 5 new purple Fords to the incoming document. for (int i = 0; i < 5; i++) { // Create a new XElement XElement newCar = new XElement("Car", new XAttribute("ID", i + 1000), new XElement("Color", "Green"), new XElement("Make", "Ford"), new XElement("PetName", "") ); // Add to doc. doc.Add(newCar); } // Show the updates. Console.WriteLine(doc); }感谢各位的阅读,以上就是"LINQ to XML的文档类型有哪些"的内容了,经过本文的学习后,相信大家对LINQ to XML的文档类型有哪些这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
文档
类型
编程
内容
模型
学习
内存
方式
程序
空间
查询
冗长
良好
三个
代码
就是
思路
情况
成员
文章
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
中国互联网科技工作委员会毛军
互联网扶贫科技
科技互联网技术
万易互联网科技
互联网科技公司怎么样
苏州互联网科技贷款公司
赫美联合互联网科技
广州善班互联网科技
互联网金融和金融科技
美国互联网前沿科技
互联网巨头投资的硬核科技
华脉科技工业互联网产品
老年人脱离互联网科技的作文
互联网校园科技培训
成都铂石互联网科技做什么的
互联网数据科技专业
互联网金融科技公司的优势
广州千川互联网科技有限公司
杭州科技互联网公司有哪些
互联网科技创新方面的资料
百单网互联网科技有限公司怎么样
奇酷互联网科技
蓝色互联网科技
金融科技与互联网金融相同和不同
黑科技闪耀互联网之光
2021互联网科技公司排名
互联网科技快讯热点
本届互联网大会上的黑科技
三亚科技创新互联网基地
互联网科技创新方面的资料