13.07.2015 Views

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

An entity reference consists of the entity name bracketed by an ampersand (&) and asemicolon (;). Not all parsers automatically expand entities upon document load<strong>in</strong>g.When the XmlTextReader class encounters an entity reference, it returns an empty<strong>in</strong>stance of the XmlEntityReference class <strong>in</strong> which the Value property is set to theempty str<strong>in</strong>g. By design, the XmlTextReader parser can't resolve entities, although itboasts a ResolveEntity method. Call<strong>in</strong>g this method always throws an exception. Youmust use XmlValidat<strong>in</strong>gReader to have entities properly expanded. (We'll covervalidat<strong>in</strong>g readers and validation schemas <strong>in</strong> Chapter 3.)Resolv<strong>in</strong>g External ReferencesIn the .<strong>NET</strong> Framework, external <strong>XML</strong> resources identified by a URI are resolvedthrough classes derived from the abstract class XmlResolver. Typical externalresources are entities and DTDs; however, the XmlResolver class can also successfullyprocess <strong>in</strong>clude and import elements <strong>for</strong> both XSD schemas and XSL style sheets.The .<strong>NET</strong> Framework provides only one concrete resolver class built atop XmlResolver:XmlUrlResolver. Programmers can design and implement custom resolvers, however,either by <strong>in</strong>herit<strong>in</strong>g from the XmlUrlResolver class or completely from scratch byoverrid<strong>in</strong>g the methods and properties of XmlResolver. Let's take a look at the keyaspects, and the ma<strong>in</strong> tasks, of a resolver.The activity of an <strong>XML</strong> resolver revolves around two methods: GetEntity andResolveUri. The <strong>for</strong>mer takes the specified URI and returns the Stream object thatrepresents the desired contents. How the method actually manages to resolve the URIis implementation-specific. GetEntity, however, assumes to have at its disposal anabsolute URI. What if the URI read from the <strong>XML</strong> document is relative? Prior to call<strong>in</strong>gGetEntity, you must be sure to call ResolveUri, pass<strong>in</strong>g both the relative URI and anybase URI. ResolveUri is responsible <strong>for</strong> comb<strong>in</strong><strong>in</strong>g these URIs <strong>in</strong>to an absolute URI.Another problem a resolver must be ready to face arises when the resource referencedby the URI is protected and available only to authenticated users. In this case, theresolver must be passed valid credentials to carry out the task. Credentials arerepresented by an <strong>in</strong>stance of the NetworkCredential class.The NetworkCredential class can be used to support a variety of authenticationschemes that make use of passwords. Among others, the list of authenticationschemes <strong>in</strong>cludes basic and digest authentication and Kerberos. The class does notsupport other types of authentication such as those based on a public key. You providethe credentials to the resolver through the XmlResolver.Credentials property, as shownhere:XmlUrlResolver resolver = new XmlUrlResolver();NetworkCredential cred = new NetworkCredential(user, pswd);resolver.Credentials = cred;reader.XmlResolver = resolver;You can also use the CredentialCache class to b<strong>in</strong>d the resolver <strong>in</strong> a s<strong>in</strong>gle shot to acollection of URI/credential pairs, as shown <strong>in</strong> the follow<strong>in</strong>g code. The collection willthen be scanned, search<strong>in</strong>g <strong>for</strong> a match<strong>in</strong>g URI each time the resolver is called toaction.CredentialCache credCache = new CredentialCache();credCache.Add(new Uri(url1), "Basic", cred);credCache.Add(new Uri(url2), "Digest", cred);resolver.Credentials = credCache;37

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!