惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 三生石上(FineUI控件)
G
Google Developers Blog
Vercel News
Vercel News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
N
Netflix TechBlog - Medium
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Engineering at Meta
Engineering at Meta
B
Blog
博客园_首页
量子位
博客园 - 叶小钗
L
LangChain Blog
T
The Blog of Author Tim Ferriss
云风的 BLOG
云风的 BLOG
Blog — PlanetScale
Blog — PlanetScale
F
Fortinet All Blogs
S
SegmentFault 最新的问题
宝玉的分享
宝玉的分享
D
DataBreaches.Net
雷峰网
雷峰网
The Cloudflare Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Last Week in AI
Last Week in AI
P
Proofpoint News Feed
TaoSecurity Blog
TaoSecurity Blog
罗磊的独立博客
MongoDB | Blog
MongoDB | Blog
The GitHub Blog
The GitHub Blog
I
Intezer
H
Help Net Security
The Hacker News
The Hacker News
The Register - Security
The Register - Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
AWS News Blog
AWS News Blog
V
V2EX
Microsoft Security Blog
Microsoft Security Blog
T
Tenable Blog
Spread Privacy
Spread Privacy
A
Arctic Wolf
P
Proofpoint News Feed
T
Threat Research - Cisco Blogs
Schneier on Security
Schneier on Security
C
CERT Recently Published Vulnerability Notes
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The Last Watchdog
The Last Watchdog
Latest news
Latest news
T
Troy Hunt's Blog
L
LINUX DO - 热门话题

博客园 - 心利

DocOptimizer 0.9.0 Beta Released SharePoint GroupedItemPicker Control Drag & Drop between SharePoint Document libraries Improving SharePoint User Experience With JQuery-Client Side Form Validate SharePoint How To:定位错误:“An SPRequest object was not disposed .." SharePoint Tips and Tricks --如何用JS向PeopleEditor填充数据 SharePoint Tips and Tricks -- Ribbon,People Editor 使用T4为数据库自动生成实体类(C#) (Tips&Tricks)用客户端模板精简JavaScript代码 (Tips &Tricks)如何为Windows Mobile 创建拨号连接--C# Windows Ce vs Windows Mobile Asp.net程序中生成Excel报表 My Page StartKit项目概览 做一个更好的程序员 .Net类库中实现的HashTable 为Asp.net控件写单元测试(ViewState) 使用Control Adapters优化Asp.net控件 ViewState 简述一(With Example And Apply to Asp.net) 桌面背景:rss新闻阅读器(图)
System.Xml FAQ Part 1
心利 · 2008-10-28 · via 博客园 - 心利

   作为一项单独的任务,最近XML小组收到一个问题列表。我们觉得这个列表很有借鉴意义,因为许多用户遇到的困难都是它们导致的。这些问题既有冷僻方法的调用,也有复杂XML的构建,但是我们将注意力集中在了一些真正难以调试的场景。当完成这个任务以后,我们想,应该把它们公布出来。

Q1: 特殊字符   

        有一些特殊字符作为保留字不能在XML中使用,比如"&"、“<”。XML标准有三种方法可以解决这个问题:转义字符、实体参考、CDATA .

        OK,这个问题对于有经验的用户来说,像是基本的XML101,但是对于新用户来说可能就是一个噩梦。问题之所以复杂化了,是因为XML 解析器通常返回令人迷惑的错误信息。这个错误也是新用户经常遇到的,因为通常待使用的文本往往包含这些特殊字符。这些字符是:

  • &   (&amp;)
  • <    (&lt;)
  • >    (&gt;)
  • ‘    (&apos;)
  • “   (&quot;)

下面是几个包含了这些特殊字符的字符串,以及相应的异常信息。这些异常都包含行号和列号,可以辅助你找到错误。

Character String

Correct Usage

Exception Message

A & B

A &amp; B

An error occurred while parsing EntityName. Line X, position Y.  

A &c B

A &amp;c B

' ' is an unexpected token. The expected token is ';'. Line X, position Y.

A &# B

A &amp;# B

Invalid syntax for a decimal numeric entity reference. Line X, position Y.

A < B

A &lt; B

Name cannot begin with the ' ' character, hexadecimal value 0x20. Line X, position Y.

Q2: XML 片段

有时候你可能需要解析一些不符合标准的XML片段,比如一个没有根节点的XML字符串,例如:

“<foo></foo><bar></bar>”

如果你把这个XML片段当做XmlReader的参数,你会得到一个异常:

“There are multiple root elements. Line X, position Y.”

这个异常设计的非常好,它准确说明了发生的错误:一个XML文档只能有一个根节点。但是如果你真的想解析这类XML该怎么做呢?看下面的代码:

string xmlFragmentStr = "<foo></foo><bar></bar>";
//create a reader settings objects and set the conformance level to

fragment
XmlReaderSettings xrs = new XmlReaderSettings();
xrs.ConformanceLevel = ConformanceLevel.Fragment;
//create a reader using the reader settings
XmlReader r = XmlReader.Create(new StringReader(xmlFragmentStr),

xrs);
r.Read();

PS One:

Some future FAQ post topics include but are not limited to:

-          Reporting Validation Warnings

-          Correct Encodings

-          ProhibitDTD setting

-          XLinq Bridge Classes

PS Two:

不知道大家有没有人对校内网感兴趣。校内网上的应用 现在也有很多很多了。小弟最近想做一个LINQ TO XiaoNei,就是一个Linq Provider,可以让开发者用C# Linq feature,操作校内网数据(用户,朋友,通知之类的)。正在恶补XML。。。。