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 / July 2006

Tip: Looking for answers? Try searching our database.

How does XmlSchemaValidationFlags.ProcessSchemaLocation actually work?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
dlutz - 26 Jul 2006 22:54 GMT
Hello to all --

   I am familiar with Xml in general and NET 1.1 Xml, but I am new to the
.NET 2.0 changes in System.Xml.

   I am trying to validate an XML document that is based on a schema that
imports several other schemas.
   I am using VS2005 and .NET 2.0 on Windows 2003 Server with SP1.
   I am using the Validate method of the XmlDocument class. I am passing in
an instance of XmlReader.

   My code is as follows (I'll explain the commented code in a second):

XmlDocument xd = new XmlDocument();
XmlReaderSettings readerSettings = new XmlReaderSettings();
//XmlSchemaSet schemaSet = new XmlSchemaSet();
XmlUrlResolver resolver = new XmlUrlResolver();

//schemaSet.Add("http://co.marin.ca.us/ComplaintRequestDocument/1.0",
"I10\\ComplaintRequest.xsd");
//schemaSet.Add("http://www.it.ojp.gov/jxdm/3.0.2",
"I10\\jxdm\\3.0.2\\jxdm.xsd");
//schemaSet.Add("http://www.it.ojp.gov/jxdm/3.0.2/proxy/ncic_2000/1.0.1",
"I10\\jxdm\\3.0.2\\proxy\\ncic_2000\\1.0.1\\ncic_2000.xsd");
//schemaSet.Add("http://www.it.ojp.gov/jxdm/3.0.2/proxy/usps_states/1.0",
"I10\\jxdm\\3.0.2\\proxy\\usps_states\\1.0\\usps_states.xsd");
//schemaSet.Add("http://www.it.ojp.gov/jxdm/3.0.2/proxy/xsd/1.0",
"I10\\jxdm\\3.0.2\\proxy\\xsd\\1.0\\xsd.xsd");
//schemaSet.Add("http://www.it.ojp.gov/jxdm/ncic_2000/1.0.1",
"I10\\jxdm\\ncic_2000\\1.0.1\\ncic_2000.xsd");
//schemaSet.Add("http://www.it.ojp.gov/jxdm/usps_states/1.0",
"I10\\jxdm\\usps_states\\1.0\\usps_states.xsd");
//readerSettings.Schemas = schemaSet;

readerSettings.ValidationType = ValidationType.Schema;
readerSettings.ValidationFlags |=
(XmlSchemaValidationFlags.ProcessSchemaLocation);
readerSettings.CloseInput = true;
readerSettings.IgnoreComments = true;
System.Net.NetworkCredential creds = new
System.Net.NetworkCredential("Administrator", <<PWD>>", "<<LOCAL MACHINE
NAME>>");
resolver.Credentials = creds;
readerSettings.XmlResolver = resolver;
XmlReader xr = XmlReader.Create(new
StringReader(xmlDoc.DocumentElement.OuterXml), readerSettings,
parserContext);
xd.Load(xr);
ValidationEventHandler eventHandler = new
ValidationEventHandler(ValidationEventHandler);
xd.Validate(eventHandler);

   As soon as I hit the last line during debugging, I get the follow
exception:

System.InvalidOperationException was unhandled
 Message="The XmlSchemaSet on the document is either null or has no schemas
in it. Provide schema information before calling Validate."
 Source="System.Xml"
 StackTrace:
      at System.Xml.XmlDocument.Validate(ValidationEventHandler
validationEventHandler, XmlNode nodeToValidate)
      at System.Xml.XmlDocument.Validate(ValidationEventHandler
validationEventHandler)
      at BizTalkAlternatives.InsertCompReqXmlTest.Main(String[] args) in
C:\Documents and Settings\dlutz\My Documents\Visual Studio
2005\Projects\BizTalkAlternatives\InsertCompReqXmlTest\InsertCompReqXmlTest.cs:line
60
      at System.AppDomain.nExecuteAssembly(Assembly assembly, String[]
args)
      at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
      at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
      at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
      at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
      at System.Threading.ThreadHelper.ThreadStart()

   From what I have been able to read about System.Xml in .NET 2.0, I
expected the validator to resolve the schemaLocation attributes (which use
relative paths) and use them to validate the document. However, that is not
what happens. What I am missing here?
   Finally, if I uncomment the code lines above where I add the schemas to
the XmlSchemaSet, all works fine and my test documents are validated as
expected.

   Thanks for suffering through this rather lengthy post. Any help here
would be appreciated.

David Lutz
Sr. Prog./Analyst
County of Marin (California)
Priya Lakshminarayanan - 28 Jul 2006 20:40 GMT
When you load the XmlDocument using a validating reader, at load time we set
the Schemas property of the XmlDocument to be the same as that of the
XmlReaderSettings object with which the validating reader was created. That
is why you commented code does not throw an exception.

But when you do not load the XmlReaderSettings object with the schemas, then
the validating reader will create its local schema set from schema locations
parsed in the xml (if the XmlSchemaValidationFlags.ProcessSchemaLocation is
set, as it is in your example) and use this for validation.
Since this schema set is not set back onto the reader settings object (as
you might be creating multiple readers using the same reader settings), the
xml document does not have access to the schemas that the reader loaded.

Hope that explains the behavior below. For XmlDocument.Validate(),
ProcessSchemaLocation, ProcessInlineSchema flags are turned off by default
and there is currently no public way to set them.

Thanks,
Priya

> Hello to all --
>
[quoted text clipped - 88 lines]
> Sr. Prog./Analyst
> County of Marin (California)

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.