Skip to Content
C# 12 核心要点
book

C# 12 核心要点

by Joseph Albahari
May 2025
Intermediate to advanced
1086 pages
14h 54m
Chinese
O'Reilly Media, Inc.
Content preview from C# 12 核心要点

第 10 章 LINQ to XML

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

NET 提供了许多处理 XML 数据的 API。通用 XML 文档处理的首选是LINQ to XML。LINQ to XML 包含一个轻量级、LINQ 友好的 XML 文档对象模型 (DOM),以及一组辅助查询操作符。

在本章中,我们将集中讨论 LINQ to XML。在第 11 章中,我们将介绍前向型 XML 读/写器,而在在线补充中,我们将介绍用于处理模式和样式表的类型。.NET 还包括传统的基于XmlDocument 的 DOM,但我们没有介绍它。

备注

LINQ to XML DOM 设计精良,性能卓越。即使不使用 LINQ,LINQ to XML DOM 作为低级XmlReaderXmlWriter 类的轻量级外观也很有价值。

所有 LINQ to XML 类型都在System.Xml.Linq 命名空间中定义。

建筑概览

本节首先简要介绍 DOM 的概念,然后解释 LINQ to XML 的 DOM 背后的原理。

什么是 DOM?

请看下面的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<customer id="123" status="archived">
  <firstname>Joe</firstname>
  <lastname>Bloggs</lastname>
</customer>

与所有 XML 文件一样,我们以一个声明开始,然后是一个根元素,其名称为customercustomer 元素有两个属性,每个属性都有名称(idstatus )和值("123""archived" )。在customer 中,有两个子元素,即firstnamelastname ,每个子元素都有简单的文本内容("Joe""Bloggs" )。

这些构造中的每一个--声明、元素、属性、值和文本内容--都可以用一个类来表示。如果这些类具有用于存储子内容的集合属性,我们就可以组合成一棵对象树来完整描述文档。这就是文档对象模型,简称 DOM。

LINQ to XML DOM

LINQ to XML 包括两个方面:

  • XML DOM,我们称之为X-DOM

  • 一组约 10 个补充查询操作符

如您所料,X-DOM 由XDocumentXElementXAttribute 等类型组成。有趣的是,X-DOM 类型与 LINQ 无关--您可以加载、实例化、更新和保存 X-DOM,而无需编写 LINQ 查询。

相反,您可以使用 LINQ 来查询由符合 W3C 标准的旧类型创建的 DOM。不过,这样做会令人沮丧,而且会造成限制。X-DOM 的显著特点是对LINQ 友好,这意味着

  • 它有一些方法可以生成有用的IEnumerable 序列,您可以对其进行查询。

  • 其构造函数的设计使您可以通过 LINQ 投影来构建 X-DOM 树。

X-DOM 概览

图 10-1显示了核心 X-DOM 类型。这些类型中最常用的是XElementXObject继承层次结构的根;XElementXDocument包含层次结构的根。

图 10-1. X-DOM 核心类型

图 10-2显示了根据以下代码创建的 X-DOM 树:

 string xml = @"<customer id='123' status='archived'> ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Programming C# 12

Programming C# 12

Ian Griffiths
C# 12 in a Nutshell

C# 12 in a Nutshell

Joseph Albahari
C# 6 for Programmers, Sixth Edition

C# 6 for Programmers, Sixth Edition

Paul Deitel, Harvey Deitel
Head First C#, 4th Edition

Head First C#, 4th Edition

Andrew Stellman, Jennifer Greene

Publisher Resources

ISBN: 9798341657038