473,396 Members | 1,714 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,396 software developers and data experts.

Accessing an XSD file

I'm trying to access a schema file as such:

Dim settings As XmlReaderSettings = New
XmlReaderSettings()
settings.Schemas.Add(webServiceNamespace, schemaFileName)
settings.ValidationType = ValidationType.Schema

Even though the webServiceNamespace value above is set to an intranet
address, I keep getting this error:

Could not find file 'C:\Program Files\Microsoft Visual Studio
8\Common7\IDE\XmlCommand.xsd

Why is it looking for the XSD file there? Is there a way to have it
look in the actual spot I put in my code?

Apr 18 '07 #1
12 3003
Doug wrote:
I'm trying to access a schema file as such:

Dim settings As XmlReaderSettings = New
XmlReaderSettings()
settings.Schemas.Add(webServiceNamespace, schemaFileName)
settings.ValidationType = ValidationType.Schema

Even though the webServiceNamespace value above is set to an intranet
address, I keep getting this error:

Could not find file 'C:\Program Files\Microsoft Visual Studio
8\Common7\IDE\XmlCommand.xsd

Why is it looking for the XSD file there? Is there a way to have it
look in the actual spot I put in my code?
The second argument to the Add method is the location. The first
argument is simply the namespace URI which is not a location. So pass in
the location as the second argument. For the first argument you can pass
in Nothing, that way the XML parser uses the namespace defined in the
schema.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Apr 18 '07 #2
That worked. But now I'm having a problem with the validate method
that I'm trying to use to validate my xml against my schema. Below is
my code and when it does the xmlDoc.Validate method I get the error:
"The XmlSchemaSet on the document is either null or has no schemas in
it. Provide schema information before calling Validate." Do you know
what might be causing that?

Dim settings As XmlReaderSettings = New XmlReaderSettings()
settings.Schemas.Add(Nothing, schemaFilePath)
settings.ValidationType = ValidationType.Schema

Dim xmlDoc As XmlDocument = New XmlDocument()
xmlDoc.LoadXml(xmlCommandString)

Dim eventHandler As ValidationEventHandler = New
ValidationEventHandler(AddressOf ValidationEventHandler)
xmlDoc.Validate(eventHandler)

Apr 18 '07 #3
Doug wrote:
Below is
my code and when it does the xmlDoc.Validate method I get the error:
"The XmlSchemaSet on the document is either null or has no schemas in
it. Provide schema information before calling Validate." Do you know
what might be causing that?

Dim settings As XmlReaderSettings = New XmlReaderSettings()
settings.Schemas.Add(Nothing, schemaFilePath)
settings.ValidationType = ValidationType.Schema

Dim xmlDoc As XmlDocument = New XmlDocument()
xmlDoc.LoadXml(xmlCommandString)
You need to use the above settings when loading the XML e.g. instead of
LoadXml use
xmlDoc.Load(XmlReader.Create(New StringReader(xmlCommandString),
settings))

Or as an alternative way, if you only want to validate after loading the
XML, you need to create an XmlSchemaSet and set xmlDoc.Schemas to that
XmlSchemaSet, after you have added your schema(s) to the schema set.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Apr 18 '07 #4
That did the trick. Thank you!

Apr 18 '07 #5
I guess I kind of lied. The code steps through just fine, but the
validation doesn't work. I tried to put some bad xml through this and
it did not do anything to indicate the xml was bad. Here is my final
code if anyone has any idea why it isn't working (I did verify that
the server.mappath function gets me the right result to where my XSD
file is)

Private Function LoadXml(ByVal xmlCommandString As String) As
XmlDocument
Dim settings As XmlReaderSettings = New XmlReaderSettings()
settings.Schemas.Add(Nothing, Server.MapPath(schemaFileName))
settings.ValidationType = ValidationType.Schema

Dim stringRdr As New StringReader(xmlCommandString)
Dim xmlRdr As XmlReader
xmlRdr = XmlReader.Create(stringRdr, settings)

Dim xmlDoc As XmlDocument = New XmlDocument()
xmlDoc.Load(xmlRdr)

Dim eventHandler As ValidationEventHandler = New
ValidationEventHandler(AddressOf ValidationEventHandler)
xmlDoc.Validate(eventHandler)

Return xmlDoc
End Function

Private Shared Sub ValidationEventHandler(ByVal sender As Object,
ByVal args As ValidationEventArgs)
Throw New XmlSchemaException(schemaFileValidationErrorMessag e &
args.Message)
End Sub
Apr 18 '07 #6
Doug wrote:
I guess I kind of lied. The code steps through just fine, but the
validation doesn't work. I tried to put some bad xml through this and
it did not do anything to indicate the xml was bad. Here is my final
code if anyone has any idea why it isn't working (I did verify that
the server.mappath function gets me the right result to where my XSD
file is)
Please post a minimal schema and XML that you are testing, then we can
say more.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Apr 18 '07 #7
This is only a portion of the schema, I haven't tested it with just
this, I tested it with the full version:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="XmlCommand" elementFormDefault="qualified"
version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://intranet/hstServices/Schemas/XmlCommand.xsd"
xmlns="http://intranet/hstServices/Schemas/XmlCommand.xsd"
xmlns:NS="http://intranet/hstServices/Schemas/XmlCommand.xsd">
<xs:attribute name="name" type="xs:string" />
<xs:element name="XmlCommand">
<xs:complexType>
<xs:sequence>
<xs:element name="Email" nillable="true">
<xs:complexType>
<xs:sequence>
<xs:element name="Success">
<xs:complexType>
<xs:sequence>
</xs:sequence>
<xs:attribute name="address" use="optional"
type="xs:string" />
<xs:attribute name="subject" use="optional"
type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="connectionKey" use="required"
type="xs:string" />
</xs:complexType>
</xs:element>
</xs:schema>

And here is my xml. I have tried removing the connectionKey value
from the beginning of the xml and it still validates just fine.

<XmlCommand connectionKey='dbStarrs'>
<Email>
<Success ad*************@ABC.com' subject='SuccessTest'/>
</Email>
</XmlCommand>

Apr 18 '07 #8
Doug wrote:
<xs:schema id="XmlCommand" elementFormDefault="qualified"
version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://intranet/hstServices/Schemas/XmlCommand.xsd"
xmlns="http://intranet/hstServices/Schemas/XmlCommand.xsd"
xmlns:NS="http://intranet/hstServices/Schemas/XmlCommand.xsd">
<xs:attribute name="name" type="xs:string" />
<xs:element name="XmlCommand">
So this schema defines an XmlCommand element in its targetNamespace
which is http://intranet/hstServices/Schemas/XmlCommand.xsd.

Your XML sample however has
<XmlCommand connectionKey='dbStarrs'>
an XmlCommand element in no namespace. What happens in this case is the
the XML parser looks at the root element, finds it is in no namespace
and then looks for a schema for no namespace. It does not have one
however and that way it simply does lax validation only.

If you set up an XmlReader with a ValidationEventHandler to report
warnings then you will get a warning that the parser does not find a
schema. However the design of the Validate method you are using is a bit
flawed as it does not allow you to get warnings, it only reports errors.

See also
<http://blogs.msdn.com/xmlteam/archive/2007/04/02/to-trust-or-not-to-trust.aspx>
which recommends "DO TURN ON the ReportValidationWarnings flag".
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Apr 19 '07 #9
Hi Martin, so if I understood you correctly part of the issue is that
my sample did not have a targetNamespace within (I think that is what
that blog was talking about too). But, I added this line of code into
my application and it did not even report the warning that the blog
talked about.

settings.ValidationFlags =
XmlSchemaValidationFlags.ReportValidationWarnings

Is there another way to do this validation then the way I'm doing it?

Apr 19 '07 #10
If I add the targetNamespace in, and then send in bad xml, it will
throw an error before it gets to the xmlDoc.Validate command (it does
it when it tries to Load the XML).

In essence, this is fine, because the error is what I am expecting,
but because it's not doing it in the location that I expect, I'm not
trapping it in the way I'd like. (Plus, I'd really like to figure out
why the .Validate won't work!) :)

Apr 19 '07 #11
Doug wrote:
Hi Martin, so if I understood you correctly part of the issue is that
my sample did not have a targetNamespace within (I think that is what
that blog was talking about too). But, I added this line of code into
my application and it did not even report the warning that the blog
talked about.

settings.ValidationFlags =
XmlSchemaValidationFlags.ReportValidationWarnings
As I tried to point out in my previous response, it is unfortunately not
possible to report warnings with the Validate method.
Is there another way to do this validation then the way I'm doing it?
Yes, the Validate method is mostly meant to Validate an XmlDocument that
has been manipulated with e.g. CreateElement/AppendChild.

If you simply want to validate your XML string you can do it directly
when parsing it e.g. (pseudo code)

Dim settings As XmlReaderSettings = New XmlReaderSettings()
settings.Schemas.Add(Nothing, schemaFilePath)
settings.ValidationType = ValidationType.Schema
settings.ValidationFlags = settings.ValidationFlags And
XmlSchemaValidationFlags.ReportValidationWarnings
AddHandler settings.ValidationEventHandler, New
ValidationEventHandler(AddressOf ValidationEventHandler)

Using Reader As XmlReader = XmlReader.Create(New
StringReader(xmlCommandString), settings)
Dim xmlDoc As XmlDocument = New XmlDocument()
xmlDoc.Load(Reader)

That way the XmlReader will do the validation and report warnings and
errors to the ValidationEventHandler.

You do not need an XmlDocument at all to do the validation, the
XmlReader would suffice plus a simple loop that reads through the XML
document but your earlier code snippets looked as if you use the
XmlDocument elsewhere so I have left that in.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Apr 19 '07 #12
That did do the trick this time. Thank you very much for your help!
Apr 19 '07 #13

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

Similar topics

3
by: Paul Phillips | last post by:
Here is what I am trying to do. I have an application that is written in VB 6 and in this application it is accessing a custom dll file that was created using Microsoft Fortran Professional 4.0....
23
by: Lamberti Fabrizio | last post by:
Hi all, I've to access to a network file from an asp pages. I've red a lot of things on old posts and on Microsoft article but I can't still solve my problem. I've got two server inside the...
3
by: prodirect | last post by:
Hi all, I hope someone can help me. I've recently created a database and wanted to put it up on an ftp sight so that multiple people could access the same tables at the same time from different...
0
by: Joergen Bech | last post by:
Fairly new to ASP.NET 1.1. Getting the error below when running application on a web server outside of my control, but only the first time I run it: 1. After a long period of inactivity (or...
5
by: Daniel Corbett | last post by:
I am trying to save a file dynamically created in a webpage. I get the following headers, but cannot figure out how to save the attachment. I am basically trying to replicate what internet...
4
by: Khalique | last post by:
I have built a web service whose purpose is to copy files from a secure place to client machine and vice versa. The problem I am having is perhaps related to permissions and access rights. For...
3
by: Olivier BESSON | last post by:
Hello, I have a web service of my own on a server (vb.net). I must declare it with SoapRpcMethod to be used with JAVA. This is a simple exemple method of my vb source : ...
4
by: raj_genius | last post by:
I hav two queries, whc are as follows: FIRSTLY: is it possible to access the controls(by name) of a parent form(MDI) from its child forms??if yes then how??plzz provide a coded example in VB if...
2
by: Vincent | last post by:
I have been trying to find some API routines that will allow me to determine the name of the computer that is accessing a file on a server. I have found the NetFileEnum call (returns the names of...
4
by: Noy B | last post by:
Hi, I have developed a small application that is using a MSAccess DB. the problem is that it was developed on a machine where the application and the DB are both located. now it needs to be...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.