473,386 Members | 1,793 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How does XmlSchemaValidationFlags.ProcessSchemaLocation actually work?

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_2 000.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_s tates.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(ValidationEventHan dler
validationEventHandler, XmlNode nodeToValidate)
at System.Xml.XmlDocument.Validate(ValidationEventHan dler
validationEventHandler)
at BizTalkAlternatives.InsertCompReqXmlTest.Main(Stri ng[] args) in
C:\Documents and Settings\dlutz\My Documents\Visual Studio
2005\Projects\BizTalkAlternatives\InsertCompReqXml Test\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.Run UsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context( Object state)
at System.Threading.ExecutionContext.Run(ExecutionCon text
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)


Jul 26 '06 #1
1 5110
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

"dlutz" <dl***@nospam.emailwrote in message
news:ur**************@TK2MSFTNGP06.phx.gbl...
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_2 000.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_s tates.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(ValidationEventHan dler
validationEventHandler, XmlNode nodeToValidate)
at System.Xml.XmlDocument.Validate(ValidationEventHan dler
validationEventHandler)
at BizTalkAlternatives.InsertCompReqXmlTest.Main(Stri ng[] args) in
C:\Documents and Settings\dlutz\My Documents\Visual Studio
2005\Projects\BizTalkAlternatives\InsertCompReqXml Test\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.Run UsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context( Object state)
at System.Threading.ExecutionContext.Run(ExecutionCon text
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)


Jul 28 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: lawrence | last post by:
Suppose I create dynamic web pages with 3 functions (which call other functions to make everything happen, but these 3 you might think of as being the top layer): registerSessions();...
5
by: me | last post by:
I have a Class Library that contains a Form and several helper classes. A thread gets created that performs processing of data behind the scenes and the Form never gets displayed (it is for debug...
38
by: Martin Marcher | last post by:
Hi, I've read several questions and often the answer was 'C knows nothing about .' So if C knows that little as some people say, what are the benefits, I mean do other languages know more...
74
by: Suyog_Linux | last post by:
I wish to know how the free()function knows how much memory to be freed as we only give pointer to allocated memory as an argument to free(). Does system use an internal variable to store allocated...
3
by: Maersa | last post by:
hi all, what does the following mean ? class A { public string @return; } also, once an object is casted to class "A" how do i recast it to a "string"
14
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it...
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
6
by: Jon Slaughter | last post by:
When I used to program in windows(back in the days of win95/98) the help system was very good. You could find out all th details of just about anything with usually decent explinations of what does...
3
by: LJB | last post by:
If I read a text file from disk into a stream how can I tell if it might already be valid XML? I'm writing an app to compare two XML files one of which may need conversion to XML first. My app will...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.