Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
XmlResolver Class
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
XmlResolver Class

Resolves external XML resources named by a Uniform Resource Identifier (URI).

Namespace:  System.Xml
Assembly:  System.Xml (in System.Xml.dll)
Visual Basic (Declaration)
Public MustInherit Class XmlResolver
Visual Basic (Usage)
Dim instance As XmlResolver
C#
public abstract class XmlResolver
Visual C++
public ref class XmlResolver abstract
JScript
public abstract class XmlResolver

XmlResolver is used to resolve external XML resources, such as entities, document type definitions (DTDs), or schemas. It is also used to process include and import elements found in Extensible StyleSheet Language (XSL) style sheets or XML Schema definition language (XSD) schemas.

XmlUrlResolver is a concrete implementation of XmlResolver and is the default resolver for all classes in the System.Xml namespace. You can also create your own resolver.

Security Considerations

Consider the following items when working with the XmlResolver class.

  • XmlResolver objects can contain sensitive information such as user credentials. You should be careful when caching XmlResolver objects and should not pass the XmlResolver object to an untrusted component.

  • If you are designing a class property that uses the XmlResolver class, the property should be defined as a write-only property. The property can be used to specify the XmlResolver to use, but it cannot be used to return an XmlResolver object.

  • If your application accepts XmlResolver objects from untrusted code, you cannot assume that the URI passed into the GetEntity method will be the same as that returned by the ResolveUri method. Classes derived from the XmlResolver class can override the GetEntity method and return data that is different than what was contained in the original URI.

  • Your application can mitigate memory Denial of Service threats to the GetEntity method by implementing a wrapping implemented IStream that limits the number of bytes read. This helps to guard against situations where malicious code attempts to pass an infinite stream of bytes to the GetEntity method.

The following example creates an XmlUrlResolver with default credentials. A XmlReader is used to read and display the resulting data stream.

Visual Basic
Imports System
Imports System.Xml
Imports System.IO

Module Module1

    Sub Main()
        ' Create an XmlUrlResolver with default credentials.
        Dim resolver As New XmlUrlResolver()
        resolver.Credentials = System.Net.CredentialCache.DefaultCredentials

        ' Point the resolver at the desired resource and resolve as a stream.
        Dim baseUri As New Uri("http://serverName/")
        Dim fulluri As Uri = resolver.ResolveUri(baseUri, "fileName.xml")
        Dim s As Stream = CType(resolver.GetEntity(fulluri, Nothing, GetType(Stream)), Stream)

        ' Create the reader with the resolved stream and display the data.
        Dim reader As XmlReader = XmlReader.Create(s)
        While reader.Read()
            Console.WriteLine(reader.ReadOuterXml())
        End While
    End Sub
End Module
C#
using System;
using System.Xml;
using System.IO;

class Example
{
    static void Main()
    {
        // Create an XmlUrlResolver with default credentials.
        XmlUrlResolver resolver = new XmlUrlResolver();
        resolver.Credentials = System.Net.CredentialCache.DefaultCredentials;

        // Point the resolver at the desired resource and resolve as a stream.
        Uri baseUri = new Uri("http://serverName/");
        Uri fulluri = resolver.ResolveUri(baseUri, "fileName.xml");
        Stream s = (Stream)resolver.GetEntity(fulluri, null, typeof(Stream));

        // Create the reader with the resolved stream and display the data.
        XmlReader reader = XmlReader.Create(s);
        while (reader.Read())
        {
            Console.WriteLine(reader.ReadOuterXml());
        }
    }
}
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
This is sample of the implementation of wrapping Stream      Sergey Dubinets - MSFT   |   Edit   |   Show History
    public class LimitedStream : Stream {
private Stream stream;
private long limit;

public LimitedStream(Stream stream, long limit) {
this.stream = stream;
this.limit = limit;
}

public override bool CanRead { get { return this.stream.CanRead ; } }
public override bool CanSeek { get { return this.stream.CanSeek ; } }
public override bool CanWrite { get { return this.stream.CanWrite; } }
public override long Length { get { return this.stream.Length ; } }

public override long Position {
get { return this.stream.Position; }
set { this.stream.Position = value; }
}

public override void Flush() {
this.stream.Flush();
}

public override int Read(byte[] buffer, int offset, int count) {
Debug.Assert(0 <= Position);
long overlimit = Position + count - (limit + 1); // +1 because we need to read beyond the limit to report an error
if (0 < overlimit) {
if (limit < Position) {
// Not interesting state but we need to check it.
throw new XmlException("Attempt to read beyond the limit ...");
}
Debug.Assert(overlimit < count);
count -= (int)overlimit;
}
int result = this.stream.Read(buffer, offset, count);
if (limit < Position) {
// It may have sence to give more info in the exception, like URI ???
throw new XmlException("Attempt to read beyond the limit ...");
}
return result;
}

public override long Seek(long offset, SeekOrigin origin) {
return this.stream.Seek(offset, origin);
}

public override void SetLength(long value) {
this.stream.SetLength(value);
}

public override void Write(byte[] buffer, int offset, int count) {
this.stream.Write(buffer, offset, count);
}
}
Processing
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker