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 核心要点

第 11 章 其他 XML 和 JSON 技术 其他 XML 和 JSON 技术

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

第 10 章中,我们介绍了 LINQ-to-XML API 以及一般的 XML。在本章中,我们将探讨底层的XmlReader/XmlWriter 类以及用于处理 JavaScript Object Notation(JSON)的类型,JSON 已成为 XML 的流行替代品。

在线补编中,我们介绍了处理 XML 架构和样式表的工具。

XmlReader

XmlReader 是一个高性能类,用于以底层、只向前的方式读取 XML 流。

请看下面的 XML 文件 customer.xml:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<customer id="123" status="archived">
  <firstname>Jim</firstname>
  <lastname>Bo</lastname>
</customer>

要实例化XmlReader ,需要调用静态XmlReader.Create 方法,传入StreamTextReader 或 URI 字符串:

using XmlReader reader = XmlReader.Create ("customer.xml");
  ...
备注

由于XmlReader 允许你从潜在的慢速源(Streams 和 URI)读取数据,因此它提供了大部分方法的异步版本,这样你就可以轻松编写非阻塞代码。我们将在第 14 章详细介绍异步功能。

构建从字符串读取数据的XmlReader

using XmlReader reader = XmlReader.Create (
  new System.IO.StringReader (myString));

您还可以通过XmlReaderSettings 对象来控制解析和验证选项。XmlReaderSettings 上的以下三个属性对于跳过多余内容特别有用:

bool IgnoreComments                  // Skip over comment nodes?
bool IgnoreProcessingInstructions    // Skip over processing instructions?
bool IgnoreWhitespace                // Skip over whitespace?

在下面的示例中,我们指示读者不要发出空白节点,因为在典型的场景中,空白节点会分散读者的注意力:

XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = true;

using XmlReader reader = XmlReader.Create ("customer.xml", settings);
  ...

XmlReaderSettings 的另一个有用属性是ConformanceLevel 。它的默认值Document 会指示阅读器假定一个有效的 XML 文档只有一个根节点。如果只想读取包含多个节点的 XML 内部部分,就会出现问题:

<firstname>Jim</firstname>
<lastname>Bo</lastname>

要在读取时不抛出异常,必须将ConformanceLevel

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