C#的LINQ to XML 类中使用最多的三个类:XElement、XAttribute 和 XDocument
最佳答案 问答题库818位专家为你答疑解惑
目录
一、XElement 类
1.使用 XElement 类创建一个 xml 文档
(1)示例源码
(2)xml文件
2.使用LINQ to SQL或者LINQ to Object获取数据源
(1)示例源码
(2)xml文件
3.XElement 类包含的其它方法
二、XAttribute 类
1.通过 XAttribute添加属性
(1)示例源码
(2)xml文件
2.使用Remove()删除属性
(1)示例源码
(2)xml文件
3.属性不是节点
(1)示例源码
(2)xml文件
三、XDocument 类
1.示例
2.生成显示
System.Xml.Linq 命名空间包含 LINQ to XML 的19个类。 LINQ to XML 是内存中的 XML 编程接口,使能轻松有效地修改 XML 文档。
微软在 LINQ 上投入了很大的精力,使我们在编程时感觉到很舒服。处理 XML 时使用最多的三个类:XElement、XAttribute 和 XDocument。
序号类说明1Extensions包含 LINQ to XML 扩展方法。2XAttribute表示 XML 特性。3XCData表示包含 CDATA 的文本节点。4XComment表示 XML 注释。5XContainer表示可包含其他节点的节点。6XDeclaration表示 XML 声明。7XDocument表示 XML 文档。 有关 XDocument 对象的组件和用法,请参阅 XDocument Class Overview。8XDocumentType表示 XML 文档类型定义 (DTD)。9XElement表示一个 XML 元素。 有关用法信息和示例,请参阅本页的 XElement 类概述和“备注”部分。10XName表示 XML 元素或属性的名称。11XNamespace表示一个 XML 命名空间。 此类不能被继承。12XNode表示 XML 树中节点的抽象概念(元素、注释、文档类型、处理指令或文本节点)。13XNodeDocumentOrderComparer包含用于比较节点文档顺序的功能。 此类不能被继承。14XNodeEqualityComparer比较节点以确定其是否相等。 此类不能被继承。15XObject表示 XML 树中的节点或属性。16XObjectChangeEventArgs提供有关 Changing 和 Changed 事件的数据。17XProcessingInstruction表示 XML 处理指令。18XStreamingElement表示 XML 树中支持流输出延迟的的元素。19XText表示文本节点。表格中列元素详解见超链接。
一、XElement 类
1.使用 XElement 类创建一个 xml 文档
(1)示例源码
//通过XElement创建XMLusing System.Xml.Linq;namespace _10_1
{class Program{static void Main(string[] args){ CreateCategories();#region 通过XElement创建XMLvoid CreateCategories(){string path = Directory.GetCurrentDirectory() + @"\People.xml";XElement root = new("Peoples",new XElement("People",new XElement("ID", Guid.NewGuid()),new XElement("Name", "王菲")),new XElement("People",new XElement("ID", Guid.NewGuid()),new XElement("Name", "谢霆锋")),new XElement("People",new XElement("ID", Guid.NewGuid()),new XElement("Name", "章子怡")),new XElement("People",new XElement("ID", Guid.NewGuid()),new XElement("Name", "汪峰")));root.Save(path);}#endregion 通过XElement创建XML}}
}
(2)xml文件
<Peoples><People><ID>9586dab0-28a4-465a-987d-5f1e89042154</ID><Name>王菲</Name></People><People><ID>7bf22551-7635-4768-bb12-d826ba0991d3</ID><Name>谢霆锋</CategoryName></People><People><ID>bcf1f65d-38f5-40f1-8ad7-eae9d7ee117e</ID><Name>章子怡</Name></People><People><ID>dc69f99b-b8cf-46c3-bba6-a23909a199cd</ID><Name>汪峰</Name></People>
</Peoples>
2.使用LINQ to SQL或者LINQ to Object获取数据源
LINQ to XML的强大之处还在于它可以使用LINQ to SQL或者LINQ to Object获取数据源,然后填充到xml树。
(1)示例源码
从 Northwind 数据库中读取 Categories、Products 表中的数据来创建包含产品类别,以及每
(2)xml文件
3.XElement 类包含的其它方法
XElement 类包含了许多方法,这些方法使得处理 xml 变得轻而易举。其中,Save、CreateReader、ToString 和 WriteTo 方法是比较常用的三个方法:
方法参数返回值描述CreateReader无System.Xml.XmlReader创建此节点的XmlReaderSayeSystem.Stringvoid将此元素序列化为文件System.I0.TextWritervoid将此元素序列化为TextWriterSystem.Xml.XmlWritervoid将此元素序列化为XmlWriterSystem.String,System.Xml.Linq.SaveOptionsvoid将此元素序列化为文件,并可以选择
禁用格式设置System.IO.TextWriter
System.Xml.Linq.SaveOptionsvoid将此元素序列化为TextWriter,并可
以选择禁用格式设置WriteToSystem.Xml.XmlWritervoid将此元素写入XmlWriterToString无System.String返回此节点的缩进XMLSystem.Xml.Ling.SaveOptionsSystem.String返回此节点的XML,并可以选择禁用
格式设置
二、XAttribute 类
XAttribute 类用来处理元素的属性,属性是与元素相关联的“名称/值”对,每个元素中不能有名称重复的属性。使用 XAttribute 类与使用 XElement 类的操作十分相似。
XAttribute 类的方法比较少,常用的三个是:
方法描述AddAnnotation为该属性添加注解Remove删除该属性SetValue设定该属性的值1.通过 XAttribute添加属性
(1)示例源码
//创建 xml 树时添加属性using System.Xml.Linq;namespace _10_1
{class Program{static void Main(string[] args){ CreateCategoriesByXAttribute();#region 创建 xml 树时添加属性XElement CreateCategoriesByXAttribute(){string path = Directory.GetCurrentDirectory() + @"\PeoplebyXAttribute.xml";XElement root = new("Peoples",new XElement("People",new XAttribute("ID", Guid.NewGuid()),new XElement("Name", "李小龙")),new XElement("People",new XAttribute("ID", Guid.NewGuid()),new XElement("Name", "李连杰")),new XElement("People",new XAttribute("ID", Guid.NewGuid()),new XElement("Name", "成龙")),new XElement("People",new XAttribute("ID", Guid.NewGuid()),new XElement("Name", "甄子丹")));root.Save(path);return root;}#endregion 创建 xml 树时添加属性}}
}
(2)xml文件
<Peoples><People ID="ed6b428c-a188-4503-870f-d4eea12c52c4"><Name>李小龙</Name></People><People ID="40cfdf39-a189-4963-a86d-e712978c4ae7"><Name>李连杰</Name></People><People ID="d3126eb3-5ede-46f3-90a7-b1d3eb5ef627"><Name>成龙</Name></People><People ID="6558808f-9ef6-4698-b05a-9747479a5238"><Name>甄子丹</Name></People>
</Peoples>
2.使用Remove()删除属性
使用 Remove 来删除第一个元素的ID 属性:
(1)示例源码
using System.IO;
using System.Xml.Linq;namespace _10_1
{class Program{static void Main(string[] args){ CreateCategoriesByXAttribute();RemoveAttribute();#region 创建 xml 树时添加属性XElement CreateCategoriesByXAttribute(){string path = Directory.GetCurrentDirectory() + @"\PeoplebyXAttribute.xml";XElement root = new("Peoples",new XElement("People",new XAttribute("ID", Guid.NewGuid()),new XElement("Name", "李小龙")),new XElement("People",new XAttribute("ID", Guid.NewGuid()),new XElement("Name", "李连杰")),new XElement("People",new XAttribute("ID", Guid.NewGuid()),new XElement("Name", "成龙")),new XElement("People",new XAttribute("ID", Guid.NewGuid()),new XElement("Name", "甄子丹")));root.Save(path);return root;}#endregion 创建 xml 树时添加属性#region 删除属性void RemoveAttribute(){string path = Directory.GetCurrentDirectory() + @"\XAttributeRemove.xml";XElement _xdoc = CreateCategoriesByXAttribute();XAttribute _attribute = _xdoc.Element("People").Attribute("ID");_attribute.Remove();_xdoc.Save(path); }# endregion 删除属性}}
}
(2)xml文件
利用Element属性和Remove()方法删除第一条记录“ID”属性
<Peoples><People><Name>李小龙</Name></People><People ID="3c5d27ca-f84d-4066-b721-cfdaeee7a90b"><Name>李连杰</Name></People><People ID="dc592933-0911-4107-bc0c-ea82563781bd"><Name>成龙</Name></People><People ID="69559674-b035-4bb6-be3a-22d9b7838a45"><Name>甄子丹</Name></People>
</Peoples>
3.属性不是节点
XAttribute 可以构造的对象与 XElement 构造的对象一致。但属性与元素之间是有些区别的。 XAttribute 对象不是 XML 树中的节点。 它们是与 XML 元素关联的名称/值对。 与文档对象模型 (DOM) 相比,这更加贴切地反映了 XML 结构。 虽然 XAttribute 对象实际上不是 XML 树的节点,但使用 XAttribute 对象与使用 XElement 对象非常相似。
(1)示例源码
using System.IO;
using System.Xml.Linq;namespace _10_1
{class Program{static void Main(string[] args){ CreateByXAttribute();#region 通过属性创建不是节点void CreateByXAttribute(){string path = Directory.GetCurrentDirectory() + @"\CreateByXAttribute.xml";XElement _c = new("Customers",new XElement("Customer",new XElement("Name", "John Doe"),new XElement("PhoneNumbers",new XElement("Phone",new XAttribute("type", "home"),"555-555-5555"),new XElement("Phone",new XAttribute("type", "work"),"666-666-6666")) // PhoneNumbers) // Customer); // CustomersConsole.WriteLine(_c);_c.Save(path);#endregion 通过属性创建不是节点}}}
}
(2)xml文件
<Customers><Customer><Name>John Doe</Name><PhoneNumbers><Phone type="home">555-555-5555</Phone><Phone type="work">666-666-6666</Phone></PhoneNumbers></Customer>
</Customers>
三、XDocument 类
1.示例
using System.Xml.Linq;namespace _10_1
{class Program{static void Main(string[] args){ CreateXmlByXDocument();#region 通过XDocument创建XMLvoid CreateXmlByXDocument(){string path = Directory.GetCurrentDirectory() + @"\CreateXmlByXDocument.xml";XDocument _doc = new(new XComment("This is a comment."),new XProcessingInstruction("xml-stylesheet", "href='mystyle.css' title='Compact' type='text/css'"),new XElement("Pubs",new XElement("Book",new XElement("Title", "Artifacts of Roman Civilization"),new XElement("Author", "Moreno, Jordao")), //Booknew XElement("Book",new XElement("Title", "Midieval Tools and Implements"),new XElement("Author", "Gazit, Inbar")) //Book), //Pubsnew XComment("This is another comment.")){Declaration = new XDeclaration("1.0", "utf-8", "true")}; //newConsole.WriteLine(_doc);_doc.Save(path);}#endregion 通过XDocument创建XML}}
}
2.生成显示
<!--This is a comment.-->
<?xml-stylesheet href='mystyle.css' title='Compact' type='text/css'?>
<Pubs><Book><Title>Artifacts of Roman Civilization</Title><Author>Moreno, Jordao</Author></Book><Book><Title>Midieval Tools and Implements</Title><Author>Gazit, Inbar</Author></Book>
</Pubs>
<!--This is another comment.-->
using System.IO;
using System.Xml.Linq;namespace _10_1
{class Program{static void Main(string[] args){ ReturnNodesAfterSelf();#region ReturnNodesAfterSelf()void ReturnNodesAfterSelf(){XElement root = new("Categories",new XElement("Category",new XElement("CategoryID", Guid.NewGuid()),new XElement("CategoryName", "食品"),new XElement("Description", "可以吃的东西")) //Category); //Categoriesforeach (var item in root.Element("Category").Element("CategoryID").NodesAfterSelf()){Console.WriteLine((item as XElement).Value);}}#endregion ReturnNodesAfterSelf()}}
}
//运行结果:
//食品
//可以吃的东西
99%的人还看了
相似问题
- SpringBoot使用ObjectMapper之Long和BigDemical类型的属性字符串处理,防止前端丢失数值精度
- 〖大前端 - 基础入门三大核心之JS篇㊲〗- DOM改变元素节点的css样式、HTML属性
- QT中样式表常见属性与颜色的设置与应用
- Java继承中的属性名相同但是类型不同的情况
- C#开发的OpenRA游戏之属性QuantizeFacingsFromSequence(7)
- XmlElement注解在Java的数组属性上,以产生多个相同的XML元素
- CSS-列表属性篇
- CSS 文本属性篇
- 计算属性与watch的区别,fetch与axios在vue中的异步请求,单文本组件使用,使用vite创建vue项目,组件的使用方法
- JAXB:用XmlElement注解复杂类型的Java属性,来产生多层嵌套的xml元素
猜你感兴趣
版权申明
本文"C#的LINQ to XML 类中使用最多的三个类:XElement、XAttribute 和 XDocument":http://eshow365.cn/6-33904-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!