常熟有哪些网站建设公司,规划设计公司简介,网站备案 通知,网站红色System.Xml.Linq 命名空间包含 LINQ to XML 的19个类。 LINQ to XML 是内存中的 XML 编程接口#xff0c;使能轻松有效地修改 XML 文档。 微软在 LINQ 上投入了很大的精力#xff0c;使我们在编程时感觉到很舒服。处理 XML 时使用最多的三个类#xff1a;XElement、XAttribu… 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 类 XElement 类是 LINQ to XML 中的基础类之一。 它表示一个 XML 元素。 可以使用该类创建元素更改元素内容添加、更改或删除子元素向元素中添加属性或以文本格式序列化元素内容。 还可以与System.Xml 中的其他类例如 XmlReader、XmlWriter 和XslCompiledTransform进行互操作。 使用 LINQ to XML 创建 xml 文档有很多种方式具体使用哪种方法要根据实际需要。而创建 xml 文档最简单、最常见的方式是使用 XElement 类。 1.使用 XElement 类创建一个 xml 文档 //通过XDocument创建XML
//通过XElement创建XMLusing System.Xml.Linq;namespace _10_1
{class Program{static void Main(string[] args){ CreateDocument();CreateCategories();#region 通过XDocument创建XMLvoid CreateDocument(){string path Directory.GetCurrentDirectory() \xmldoc.xml;XDocument _xmldoc new(new XDeclaration(1.0, utf-8, yes),new XElement(Root, root));_xmldoc.Save(path);}#endregion 通过XDocument创建XML#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.输出文件
1使用 XElement 类创建一个 xml 文档
PeoplesPeopleID9586dab0-28a4-465a-987d-5f1e89042154/IDName王菲/Name/PeoplePeopleID7bf22551-7635-4768-bb12-d826ba0991d3/IDName谢霆锋/CategoryName/PeoplePeopleIDbcf1f65d-38f5-40f1-8ad7-eae9d7ee117e/IDName章子怡/Name/PeoplePeopleIDdc69f99b-b8cf-46c3-bba6-a23909a199cd/IDName汪峰/Name/People
/Peoples
2使用 XDocument类创建一个 xml 文档
Rootroot/Root
3.使用LINQ to SQL或者LINQ to Object获取数据源 LINQ to XML的强大之处还在于它可以使用LINQ to SQL或者LINQ to Object获取数据源然后填充到xml树。
1示例源码 从 Northwind 数据库中读取 Categories、Products 表中的数据来创建包含产品类别以及每 个类别下所有产品的 xml 文件。 2输出文件 4.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 类的操作十分相似。
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输出文件
PeoplesPeople IDed6b428c-a188-4503-870f-d4eea12c52c4Name李小龙/Name/PeoplePeople ID40cfdf39-a189-4963-a86d-e712978c4ae7Name李连杰/Name/PeoplePeople IDd3126eb3-5ede-46f3-90a7-b1d3eb5ef627Name成龙/Name/PeoplePeople ID6558808f-9ef6-4698-b05a-9747479a5238Name甄子丹/Name/People
/Peoples
三、XDocument 类 XDocument 类提供了处理 xml 文档的方法包括声明、注释和处理指令。一个 XDocument 对象可以包含以下内容 对象个数说明XDeclaration一个用于指定 xml 声明中的重要组成部分如文档编码和版本等XElement一个指定文档的根元素XDocumentType一个表示一个 xml DTDXComment多个Xml 注释。它不能是第一个参数因为一个有效的 xml 文档不能以注释作为开始XProcessingInstruction多个为处理 xml 的应用程序指定任何所需信息