Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / September 2004

Tip: Looking for answers? Try searching our database.

XmlSchema.Compile() method

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Benson Cheng - 18 Sep 2004 00:39 GMT
I am trying to load a XML schema which includes other schemas by using the
XmlSchema.Read and XmlSchema.Compile() methods, from the .NET v1.1
documentation, there is overloaded Compile() method that takes a XmlResolver
parameter

public void Compile(
  ValidationEventHandler validationEventHandler,
  XmlResolver resolver
);

But when I use this methods with a my derived XmlResolver (MyXmlResolver)
object, during the schema compilation, the MyXmlResolver.ResolveUri() and
MyXmlResolver.GetEntity() methods never called, I thought the purpose of the
XmlResolver is during the schema compiliation, if there is any xs:include or
xs:import elements in the schema, the XmlResolver.ResolveUri() methods will
get called to resolve the included/imported schema URI.  Did I misunderstand
the usage of the XmlResolver?  If yes, then what's the best way to load
schema that includes or imports other schemas?

Thanks,
Benson.
Zafar Abbas [MSFT] - 18 Sep 2004 02:15 GMT
Could you please post the code that you are using?

Zafar

> I am trying to load a XML schema which includes other schemas by using the
> XmlSchema.Read and XmlSchema.Compile() methods, from the .NET v1.1
[quoted text clipped - 17 lines]
> Thanks,
> Benson.
Benson Cheng - 20 Sep 2004 16:43 GMT
Here is the XmlResolver deriven class, just for test purpose, it return null

class FileUriResolver : XmlResolver
{
public FileUriResolver() :
 base()
{
}

public override ICredentials Credentials
{
 set
 {
  System.Console.WriteLine("FileUriResolver - set Credentials called");
 }
}

public override object GetEntity(Uri absoluteUri, string role, Type
ofObjectToReturn)
{
 System.Console.WriteLine("absoluteUri=" + absoluteUri.AbsolutePath + ",
role=" + role + ", ofObjectToReturn=" + ofObjectToReturn.ToString());
 return null;
}

public override Uri ResolveUri(Uri baseUri, string relativeUri)
{
 System.Console.WriteLine("baseUri=" + baseUri.AbsolutePath + ",
relativeUri=" + relativeUri);
 return null;
}
}

Here is the code that read and compile the schema

public void Load(string schemaName)
{
Stream s = new FileStream(schemaName, FileMode.Open, FileAccess.Read);

ValidationEventHandler eventHandler = new
ValidationEventHandler(this.ValidationHandler);
XmlSchema schema = XmlSchema.Read(s, eventHandler);

schema.Compile(eventHandler, new FileUriResolver());
}

When I run it, I didn't see any of the Console.WriteLine print out, and I am
getting these events from ValidationHandler:

Warning:Cannot resolve schemaLocation attribute. An error occurred at , (19,
3).
....
Error:Type 'PartnerDescription_type5' is not declared. An error occurred at
, (177, 6).
....

The 'PartnerDescription_type5' is a complex type defined in the one of the
include schema.

thanks,
Benson.

> Could you please post the code that you are using?
>
[quoted text clipped - 26 lines]
> > Thanks,
> > Benson.
Zafar Abbas [MSFT] - 21 Sep 2004 06:31 GMT
Since your overridden GetEntity and ResolveUri methods return null, the
schema processor is not able to resolve the included schema, that is the
reason of throwing the undeclared type errors.

> Here is the XmlResolver deriven class, just for test purpose, it return null
>
[quoted text clipped - 92 lines]
> > > Thanks,
> > > Benson.
Benson Cheng - 21 Sep 2004 17:33 GMT
Sorry, may be I didn't make it clear in my previous post, I have
System.Console.WriteLine in both methods, so if any one of the methods get
called by the schema processor, then I should see these WriteLine on the
console.  But when I ran the following code as a console application, I
didn't see any of the System.Console.WriteLine output at all, which means
these methods never get called by the schema processor.

Thanks,
Benson.

> Since your overridden GetEntity and ResolveUri methods return null, the
> schema processor is not able to resolve the included schema, that is the
[quoted text clipped - 102 lines]
> > > > Thanks,
> > > > Benson.
Zafar Abbas [MSFT] - 21 Sep 2004 18:11 GMT
When I tried your sample, I had changed your methods as below. Since you are
parsing the XSD by a Stream, thebaseURI of the schema is null, which is why
the Console did not print.

public override object GetEntity(Uri absoluteUri, string role, Type
ofObjectToReturn)

{

//Console.WriteLine("absoluteUri=" + absoluteUri.AbsolutePath + ",role=" +
role + ", ofObjectToReturn=" + ofObjectToReturn.ToString());

Console.WriteLine("getentity called");

return null;

}

public override Uri ResolveUri(Uri baseUri, string relativeUri)

{

//Console.WriteLine("baseUri=" + baseUri.AbsolutePath + ",relativeUri=" +
relativeUri);

Console.WriteLine("ResolveUri called");

return null;

}

> Sorry, may be I didn't make it clear in my previous post, I have
> System.Console.WriteLine in both methods, so if any one of the methods get
[quoted text clipped - 119 lines]
> > > > > Thanks,
> > > > > Benson.
Benson Cheng - 21 Sep 2004 21:25 GMT
Yeah,  you are right.  I guess the NullReferenceException caught and handled
by the schema processor sliently.

How can I set the schema's baseUri?  I tried set schema.SourceUri, but that
didn't work.

thanks for you help.
Benson.

> When I tried your sample, I had changed your methods as below. Since you are
> parsing the XSD by a Stream, thebaseURI of the schema is null, which is why
[quoted text clipped - 156 lines]
> > > > > > Thanks,
> > > > > > Benson.
Zafar Abbas [MSFT] - 21 Sep 2004 23:36 GMT
Use this

XmlSchema schema = XmlSchema.Read(new XmlTextReader(schemaName),
eventHandler);

to create the XmlSchema object

> Yeah,  you are right.  I guess the NullReferenceException caught and handled
> by the schema processor sliently.
[quoted text clipped - 177 lines]
> > > > > > > Thanks,
> > > > > > > Benson.
Benson Cheng - 22 Sep 2004 01:02 GMT
Zafar,

Thanks for your help.  I got it handle now.

Benson.

> Use this
>
[quoted text clipped - 197 lines]
> > > > > > > > Thanks,
> > > > > > > > Benson.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.