Relative XSD path

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Aleksey Timonin

    Relative XSD path

    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

    #2
    Re: Relative XSD path

    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


    Comment

    • Aleksey Timonin

      #3
      Re: Relative XSD path

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

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

      Thanks a lot,
      Aleksey

      "Marc Gravell" <marc.gravell@g mail.comwrote in message
      news:%230SPpbIb IHA.208@TK2MSFT NGP02.phx.gbl.. .
      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
      >

      Comment

      • Marc Gravell

        #4
        Re: Relative XSD path

        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


        Comment

        • Aleksey Timonin

          #5
          Re: Relative XSD path

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

          it fails with the next exception:

          System.NullRefe renceException: 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

          "Marc Gravell" <marc.gravell@g mail.comwrote in message
          news:OPF6OwIbIH A.4712@TK2MSFTN GP04.phx.gbl...
          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
          >

          Comment

          • Marc Gravell

            #6
            Re: Relative XSD path

            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.Creat e("Foo/Bar/Outer.xsd")) {
            schema = new XmlSchemaSet();
            schema.Add(XmlS chema.Read(read er, Oops));
            schema.Compile( );
            }
            XmlReaderSettin gs settings = new XmlReaderSettin gs();
            settings.Schema s = schema;
            settings.Valida tionEventHandle r += Oops;
            settings.Valida tionType = ValidationType. Schema;
            using (XmlReader reader = XmlReader.Creat e("Sample.xml ",
            settings)) {
            while (reader.Read()) { }
            }

            Marc


            Comment

            • Aleksey Timonin

              #7
              Re: Relative XSD path

              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


              "Marc Gravell" <marc.gravell@g mail.comwrote in message
              news:e6P2sQJbIH A.4344@TK2MSFTN GP02.phx.gbl...
              >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.Creat e("Foo/Bar/Outer.xsd"))
              {
              schema = new XmlSchemaSet();
              schema.Add(XmlS chema.Read(read er, Oops));
              schema.Compile( );
              }
              XmlReaderSettin gs settings = new XmlReaderSettin gs();
              settings.Schema s = schema;
              settings.Valida tionEventHandle r += Oops;
              settings.Valida tionType = ValidationType. Schema;
              using (XmlReader reader = XmlReader.Creat e("Sample.xml ",
              settings)) {
              while (reader.Read()) { }
              }
              >
              Marc
              >

              Comment

              • Marc Gravell

                #8
                Re: Relative XSD path

                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


                Comment

                • Marc Gravell

                  #9
                  Re: Relative XSD path

                  (for completeness, this is the XmlReader.BaseU RI 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


                  Comment

                  Working...