Relative XSD path

A

Aleksey Timonin

Hi guys,
I want to validate my xml by xsd schema. But in the xsd file I have RELATIVE
includes to the others xsd files that located in ..\ directory for the main
xsd.
Could you provide me code example that makes such a validation. All the
examples I found in google doesn't work.

Thanks a lot,
Aleksey
 
M

Marc Gravell

What environment? asp.net? standard exe? etc?

If I recall, this should simply work (I'll run a quick test in a
moment to check) - however, it isn't hard to roll a custom XmlResolver
if you want to take control of where nested xsd come from; I use such
to pull my xsd from a resource file, for example.

Marc
 
A

Aleksey Timonin

I work in winforms environment.
I have main xsd that includes another in the next way:
<xs:include schemaLocation="..\MessageHeader.xsd"/>

Would you please send me your test code, that will resolve me the problem?

Thanks a lot,
Aleksey
 
M

Marc Gravell

Try swapping the slash; the following works fine for me:

<xs:include schemaLocation="../Inner.xsd"/>

Let me know how you get on; I have a working console exe that works
fine with this approach, but I don't want to swamp you with xsd, xml
and cs unless the above fails ;-p

Marc
 
A

Aleksey Timonin

No it doesn't help :(
Still when I call
reader = XmlReader.Create( new StringReader( xml ), settings );

it fails with the next exception:

System.NullReferenceException: Object reference not set to an instance of an
object
Would you please provide me your test code, so I'll check what don't I do
right?

Thank you a lot for your help!!!
Aleksey
 
M

Marc Gravell

I have (where there is also a Foo/Inner.xsd, included as per the
previous post); my "Oops" handler just writes a message to the console
to verify that my xml has been processed correctly (some valid, some
invalid):

XmlSchemaSet schema;
using(XmlReader reader =
XmlReader.Create("Foo/Bar/Outer.xsd")) {
schema = new XmlSchemaSet();
schema.Add(XmlSchema.Read(reader, Oops));
schema.Compile();
}
XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas = schema;
settings.ValidationEventHandler += Oops;
settings.ValidationType = ValidationType.Schema;
using (XmlReader reader = XmlReader.Create("Sample.xml",
settings)) {
while (reader.Read()) { }
}

Marc
 
A

Aleksey Timonin

Thanks a lot, Marc!
It works now.
The problem was the next: I had pretty the same code with one defferance
that I used in StreamReader instead of XmlReader in the first "using"
statement.

Thank you a lot once again!
Aleksey
 
M

Marc Gravell

No problem; I can reproduce what you are seeing (StreamReader
failing); at a guess, perhaps the StreamReader doesn't have as much
clue where the original source location is, so it can't use
XmlResolver to find related files...

Interesting; I learn't something there (don't use StreamReader here
;-p) - so thanks for that.

Cheers,

Marc
 
M

Marc Gravell

(for completeness, this is the XmlReader.BaseURI property; there is
nothing equivalent for raw streams - plus you've gotta love the
consistent casing [URI vs Uri] and return type [System.String vs
System.Uri])

Marc
 

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

Top