newbie: XmlUrlResolver?

D

Dan

Hi, I'm creating a C# assembly which loads from its resources some XSDL
schemas and use them to validate external XML files. Some of these schemas
refer to other schemas via include commands. How can I let the validator
resolve these references in schemas loaded from assembly resources? I think
I should use an XmlUrlResolver, but I do not know how it works.
Here's an attempt with this code snippet, let's say that the 2 schemas are
named A.xsd and B.xsd, both placed as embedded resources in the Dummy
assembly: should I add both to a schema collection, and/or use a resolver
(how?), or what? Could anyone give a hint? Thx guys!

// create 2 streams for 2 schemas from this assembly resource
Assembly a = Assembly.GetExecutingAssembly();
Stream stA = a.GetManifestResourceStream("Dummy.A.xsd");
Stream stB = a.GetManifestResourceStream("Dummy.B.xsd");

// create a resolver?
XmlUrlResolver resolver = new XmlUrlResolver();
System.Net.NetworkCredential nc;
nc = new System.Net.NetworkCredential(... what to do here...?);
resolver.Credentials = nc;

// create a schema collection?
XmlSchemaCollection xsc = new XmlSchemaCollection();
XmlTextReader rdA = new XmlTextReader(new StreamReader(stA,
System.Text.Encoding.UTF8));
xsc.Add(null, rdA, resolver);
XmlTextReader rdB = new XmlTextReader(new StreamReader(stB,
System.Text.Encoding.UTF8));
xsc.Add(null, rdB, resolver);

// ... validate with a XmlValidatingReader using xsc ...
 
J

Jeffrey Tan[MSFT]

Hi Dan,

You can find how to resolve external xml resources from the links below:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconresolveexternalxmlresourcesnamedbyuri.asp
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconresolvingresources
usingxmlresolver.asp

You can find how to supply Authentication Credentials from:
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconsupplyingauthentic
ationcredentialstoxmlresolverwhenreadingfromfile.asp

The link below tells you how to create custom resolver:
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconcreatingcustomreso
lver.asp

Hope all these help

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Dan" <[email protected]>
| Subject: newbie: XmlUrlResolver?
| Date: Mon, 10 Nov 2003 19:31:34 +0100
| Lines: 33
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: ppp-217-133-157-90.cust-adsl.tiscali.it 217.133.157.90
| Path:
cpmsftngxa06.phx.gbl!cpmsftngxa09.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.
phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:198128
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Hi, I'm creating a C# assembly which loads from its resources some XSDL
| schemas and use them to validate external XML files. Some of these schemas
| refer to other schemas via include commands. How can I let the validator
| resolve these references in schemas loaded from assembly resources? I
think
| I should use an XmlUrlResolver, but I do not know how it works.
| Here's an attempt with this code snippet, let's say that the 2 schemas are
| named A.xsd and B.xsd, both placed as embedded resources in the Dummy
| assembly: should I add both to a schema collection, and/or use a resolver
| (how?), or what? Could anyone give a hint? Thx guys!
|
| // create 2 streams for 2 schemas from this assembly resource
| Assembly a = Assembly.GetExecutingAssembly();
| Stream stA = a.GetManifestResourceStream("Dummy.A.xsd");
| Stream stB = a.GetManifestResourceStream("Dummy.B.xsd");
|
| // create a resolver?
| XmlUrlResolver resolver = new XmlUrlResolver();
| System.Net.NetworkCredential nc;
| nc = new System.Net.NetworkCredential(... what to do here...?);
| resolver.Credentials = nc;
|
| // create a schema collection?
| XmlSchemaCollection xsc = new XmlSchemaCollection();
| XmlTextReader rdA = new XmlTextReader(new StreamReader(stA,
| System.Text.Encoding.UTF8));
| xsc.Add(null, rdA, resolver);
| XmlTextReader rdB = new XmlTextReader(new StreamReader(stB,
| System.Text.Encoding.UTF8));
| xsc.Add(null, rdB, resolver);
|
| // ... validate with a XmlValidatingReader using xsc ...
|
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top