Accessing an XSD file

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

    Accessing an XSD file

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

    Dim settings As XmlReaderSettin gs = New
    XmlReaderSettin gs()
    settings.Schema s.Add(webServic eNamespace, schemaFileName)
    settings.Valida tionType = ValidationType. Schema

    Even though the webServiceNames pace 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\X mlCommand.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?

  • Martin Honnen

    #2
    Re: Accessing an XSD file

    Doug wrote:
    I'm trying to access a schema file as such:
    >
    Dim settings As XmlReaderSettin gs = New
    XmlReaderSettin gs()
    settings.Schema s.Add(webServic eNamespace, schemaFileName)
    settings.Valida tionType = ValidationType. Schema
    >
    Even though the webServiceNames pace 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\X mlCommand.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

    Comment

    • Doug

      #3
      Re: Accessing an XSD file

      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 XmlReaderSettin gs = New XmlReaderSettin gs()
      settings.Schema s.Add(Nothing, schemaFilePath)
      settings.Valida tionType = ValidationType. Schema

      Dim xmlDoc As XmlDocument = New XmlDocument()
      xmlDoc.LoadXml( xmlCommandStrin g)

      Dim eventHandler As ValidationEvent Handler = New
      ValidationEvent Handler(Address Of ValidationEvent Handler)
      xmlDoc.Validate (eventHandler)

      Comment

      • Martin Honnen

        #4
        Re: Accessing an XSD file

        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 XmlReaderSettin gs = New XmlReaderSettin gs()
        settings.Schema s.Add(Nothing, schemaFilePath)
        settings.Valida tionType = ValidationType. Schema
        >
        Dim xmlDoc As XmlDocument = New XmlDocument()
        xmlDoc.LoadXml( xmlCommandStrin g)
        You need to use the above settings when loading the XML e.g. instead of
        LoadXml use
        xmlDoc.Load(Xml Reader.Create(N ew StringReader(xm lCommandString) ,
        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

        Comment

        • Doug

          #5
          Re: Accessing an XSD file

          That did the trick. Thank you!

          Comment

          • Doug

            #6
            Re: Accessing an XSD file

            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 xmlCommandStrin g As String) As
            XmlDocument
            Dim settings As XmlReaderSettin gs = New XmlReaderSettin gs()
            settings.Schema s.Add(Nothing, Server.MapPath( schemaFileName) )
            settings.Valida tionType = ValidationType. Schema

            Dim stringRdr As New StringReader(xm lCommandString)
            Dim xmlRdr As XmlReader
            xmlRdr = XmlReader.Creat e(stringRdr, settings)

            Dim xmlDoc As XmlDocument = New XmlDocument()
            xmlDoc.Load(xml Rdr)

            Dim eventHandler As ValidationEvent Handler = New
            ValidationEvent Handler(Address Of ValidationEvent Handler)
            xmlDoc.Validate (eventHandler)

            Return xmlDoc
            End Function

            Private Shared Sub ValidationEvent Handler(ByVal sender As Object,
            ByVal args As ValidationEvent Args)
            Throw New XmlSchemaExcept ion(schemaFileV alidationErrorM essage &
            args.Message)
            End Sub


            Comment

            • Martin Honnen

              #7
              Re: Accessing an XSD file

              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

              Comment

              • Doug

                #8
                Re: Accessing an XSD file

                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 " elementFormDefa ult="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="XmlComman d">
                <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="connectio nKey" 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 address='DWhite @ABC.com' subject='Succes sTest'/>
                </Email>
                </XmlCommand>

                Comment

                • Martin Honnen

                  #9
                  Re: Accessing an XSD file

                  Doug wrote:
                  <xs:schema id="XmlCommand " elementFormDefa ult="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="XmlComman d">
                  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 ValidationEvent Handler 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 ReportValidatio nWarnings flag".


                  --

                  Martin Honnen --- MVP XML

                  Comment

                  • Doug

                    #10
                    Re: Accessing an XSD file

                    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.Valida tionFlags =
                    XmlSchemaValida tionFlags.Repor tValidationWarn ings

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

                    Comment

                    • Doug

                      #11
                      Re: Accessing an XSD file

                      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!) :)

                      Comment

                      • Martin Honnen

                        #12
                        Re: Accessing an XSD file

                        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.Valida tionFlags =
                        XmlSchemaValida tionFlags.Repor tValidationWarn ings
                        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 XmlReaderSettin gs = New XmlReaderSettin gs()
                        settings.Schema s.Add(Nothing, schemaFilePath)
                        settings.Valida tionType = ValidationType. Schema
                        settings.Valida tionFlags = settings.Valida tionFlags And
                        XmlSchemaValida tionFlags.Repor tValidationWarn ings
                        AddHandler settings.Valida tionEventHandle r, New
                        ValidationEvent Handler(Address Of ValidationEvent Handler)

                        Using Reader As XmlReader = XmlReader.Creat e(New
                        StringReader(xm lCommandString) , settings)
                        Dim xmlDoc As XmlDocument = New XmlDocument()
                        xmlDoc.Load(Rea der)

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

                        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

                        Comment

                        • Doug

                          #13
                          Re: Accessing an XSD file

                          That did do the trick this time. Thank you very much for your help!


                          Comment

                          Working...