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 / Languages / C# / February 2008

Tip: Looking for answers? Try searching our database.

Relative XSD path

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Aleksey Timonin - 11 Feb 2008 07:45 GMT
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
Marc Gravell - 11 Feb 2008 08:23 GMT
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
Aleksey Timonin - 11 Feb 2008 08:44 GMT
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

> What environment? asp.net? standard exe? etc?
>
[quoted text clipped - 4 lines]
>
> Marc
Marc Gravell - 11 Feb 2008 08:59 GMT
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
Aleksey Timonin - 11 Feb 2008 09:11 GMT
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

> Try swapping the slash; the following works fine for me:
>
[quoted text clipped - 5 lines]
>
> Marc
Marc Gravell - 11 Feb 2008 09:57 GMT
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
Aleksey Timonin - 11 Feb 2008 11:51 GMT
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

>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
[quoted text clipped - 17 lines]
>
> Marc
Marc Gravell - 11 Feb 2008 12:02 GMT
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
Marc Gravell - 11 Feb 2008 12:19 GMT
(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

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.