Upgrading XMLValidatingReader

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?SlQ=?=

    Upgrading XMLValidatingReader

    Hi all,
    As you know, the XMLValidatingRe ader is deprecated in .NET 2.0. I have the
    following snippet from a .NET 1.1 implementation that I am struggling to
    upgrade. I know I should use XMLReader.Creat e, but am struggling to get much
    further. Any help would be appreciated. Thanks.

    Public Shared Sub ValidateDocumen t(businessDocum ent As XmlDocument,
    schemaStrongNam e As String)
    ' Constants
    Const PARTS_IN_SCHEMA _STRONG_NAME As Integer = 2
    Const PART_CLASS_NAME As Integer = 0
    Const PART_QUALIFIED_ ASSEMBLY_NAME As Integer = 1

    ' Parse schema strong name
    Dim assemblyNamePar ts As String() = schemaStrongNam e.Split(New Char()
    {","c}, PARTS_IN_SCHEMA _STRONG_NAME)
    Dim className As String = assemblyNamePar ts(PART_CLASS_N AME).Trim()
    Dim fullyQualifiedA ssemblyName As String =
    assemblyNamePar ts(PART_QUALIFI ED_ASSEMBLY_NAM E).Trim()

    ' Load assembly
    Dim schemaAssembly As [Assembly] =
    [Assembly].Load(fullyQual ifiedAssemblyNa me)

    ' Create instance of the BTS schema in order to get to the actual schemas
    Dim schemaType As Type = schemaAssembly. GetType(classNa me)
    Dim btsSchemaCollec tion As Microsoft.XLANG s.BaseTypes.Sch emaBase =
    CType(Activator .CreateInstance (schemaType),
    Microsoft.XLANG s.BaseTypes.Sch emaBase)

    ' Set up XML validating reader and validate document
    Dim parserContext As New XmlParserContex t(Nothing, Nothing, "",
    XmlSpace.None)
    Dim reader As New XmlValidatingRe ader(businessDo cument.OuterXml ,
    XmlNodeType.Doc ument, parserContext)
    reader.Validati onType = ValidationType. Schema
    reader.Schemas. Add(btsSchemaCo llection.Schema Collection)
    While reader.Read()
    End While
    End Sub 'ValidateDocume nt

    --
    John
  • =?Utf-8?B?SlQ=?=

    #2
    RE: Upgrading XMLValidatingRe ader

    A little more info...
    This is what I have come up with so far..

    ' Create instance of the BTS schema in order to get to the actual schemas
    Dim schemaType As Type = schemaAssembly. GetType(classNa me)
    Dim btsSchemaCollec tion As Microsoft.XLANG s.BaseTypes.Sch emaBase =
    CType(Activator .CreateInstance (schemaType),
    Microsoft.XLANG s.BaseTypes.Sch emaBase)
    ' Create the XmlSchemaSet class.
    Dim sc As New XmlSchemaSet
    ' Add the schema to the collection.
    sc.Add(btsSchem aCollection.Sch ema)

    ' Set the validation settings.
    Dim settings As XmlReaderSettin gs = New XmlReaderSettin gs()
    settings.Valida tionType = ValidationType. Schema
    settings.Schema s = sc
    'AddHandler settings.Valida tionEventHandle r, AddressOf
    ValidationCallB ack

    ' Create the XmlReader object.
    Using reader As XmlReader =
    XmlReader.Creat e(businessDocum ent.SchemaInfo. SchemaAttribute .SchemaType.Sou rceUri, settings)

    ' Parse the file.
    While reader.Read()
    End While

    End Using


    When I run the app, I get an error that the parameter inputURI was not
    recognized as a valid URI. Thus, this is the problem statement.

    Using reader As XmlReader =
    XmlReader.Creat e(businessDocum ent.SchemaInfo. SchemaAttribute .SchemaType.Sou rceUri, settings)

    I have tried all kinds of baseURI, NamespaceURI, SourceURI combos and just
    can't seem to get it. So, my very specific question is

    How do I specify the inputURI for the document to be validated in creating
    the XMLReader? This does NOT work
    "businessDocume nt.SchemaInfo.S chemaAttribute. SchemaType.Sour ceUri"

    Thanks


    --
    John


    "JT" wrote:
    Hi all,
    As you know, the XMLValidatingRe ader is deprecated in .NET 2.0. I have the
    following snippet from a .NET 1.1 implementation that I am struggling to
    upgrade. I know I should use XMLReader.Creat e, but am struggling to get much
    further. Any help would be appreciated. Thanks.
    >
    Public Shared Sub ValidateDocumen t(businessDocum ent As XmlDocument,
    schemaStrongNam e As String)
    ' Constants
    Const PARTS_IN_SCHEMA _STRONG_NAME As Integer = 2
    Const PART_CLASS_NAME As Integer = 0
    Const PART_QUALIFIED_ ASSEMBLY_NAME As Integer = 1
    >
    ' Parse schema strong name
    Dim assemblyNamePar ts As String() = schemaStrongNam e.Split(New Char()
    {","c}, PARTS_IN_SCHEMA _STRONG_NAME)
    Dim className As String = assemblyNamePar ts(PART_CLASS_N AME).Trim()
    Dim fullyQualifiedA ssemblyName As String =
    assemblyNamePar ts(PART_QUALIFI ED_ASSEMBLY_NAM E).Trim()
    >
    ' Load assembly
    Dim schemaAssembly As [Assembly] =
    [Assembly].Load(fullyQual ifiedAssemblyNa me)
    >
    ' Create instance of the BTS schema in order to get to the actual schemas
    Dim schemaType As Type = schemaAssembly. GetType(classNa me)
    Dim btsSchemaCollec tion As Microsoft.XLANG s.BaseTypes.Sch emaBase =
    CType(Activator .CreateInstance (schemaType),
    Microsoft.XLANG s.BaseTypes.Sch emaBase)
    >
    ' Set up XML validating reader and validate document
    Dim parserContext As New XmlParserContex t(Nothing, Nothing, "",
    XmlSpace.None)
    Dim reader As New XmlValidatingRe ader(businessDo cument.OuterXml ,
    XmlNodeType.Doc ument, parserContext)
    reader.Validati onType = ValidationType. Schema
    reader.Schemas. Add(btsSchemaCo llection.Schema Collection)
    While reader.Read()
    End While
    End Sub 'ValidateDocume nt
    >
    --
    John

    Comment

    • Kevin Yu [MSFT]

      #3
      RE: Upgrading XMLValidatingRe ader

      Hi John,

      Based on my understanding, you're trying to validate an Xml given an
      XmlDocument. If there is any misunderstandin g, please feel free to let me
      know.

      In .NET 2.0, schema validation is supported by XmlDocument, you can use
      XmlDocument.Val idate to validate it directly. Here is a good example.

      Validates the XmlDocument against the XML Schema Definition Language (XSD) schemas contained in the Schemas property.


      Kevin Yu
      Microsoft Online Community Support

      =============== =============== =============== =====
      Get notification to my posts through email? Please refer to
      Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.

      ications.
      Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
      where an initial response from the community or a Microsoft Support
      Engineer within 1 business day is acceptable. Please note that each follow
      up response may take approximately 2 business days as the support
      professional working with you may need further investigation to reach the
      most efficient resolution. The offering is not appropriate for situations
      that require urgent, real-time or phone-based interactions or complex
      project analysis and dump analysis issues. Issues of this nature are best
      handled working with a dedicated Microsoft Support Engineer by contacting
      Microsoft Customer Support Services (CSS) at
      http://msdn.microsoft.com/subscripti...t/default.aspx.
      =============== =============== =============== =====

      (This posting is provided "AS IS", with no warranties, and confers no
      rights.)

      Comment

      • Kevin Yu [MSFT]

        #4
        RE: Upgrading XMLValidatingRe ader

        Hi John,

        The example in the document uses XmlReader.Creat e because it need to read
        from a reader. You can directly load from a file or something else. Here I
        wrote an example on my machine and hope it helps.

        private void Form1_Load(obje ct sender, EventArgs e)
        {
        XmlSchema sch = XmlSchema.Read( new
        XmlTextReader(@ "c:\xmlxsdProbl em.xsd"), null);
        XmlDocument doc = new XmlDocument();
        doc.Schemas.Add (sch);
        doc.Load(@"c:\x mlxsdProblem.xm l");
        ValidationEvent Handler a = new
        ValidationEvent Handler(Validat ionhandler);
        doc.Validate(a) ;
        }

        public void Validationhandl er(Object sender, ValidationEvent Args e)
        {
        //....
        }

        Kevin Yu
        Microsoft Online Community Support
        =============== =============== =============== =====

        (This posting is provided "AS IS", with no warranties, and confers no
        rights.)

        Comment

        • =?Utf-8?B?SlQ=?=

          #5
          RE: Upgrading XMLValidatingRe ader

          Thank you very much Kevin.
          --
          John


          "Kevin Yu [MSFT]" wrote:
          Hi John,
          >
          The example in the document uses XmlReader.Creat e because it need to read
          from a reader. You can directly load from a file or something else. Here I
          wrote an example on my machine and hope it helps.
          >
          private void Form1_Load(obje ct sender, EventArgs e)
          {
          XmlSchema sch = XmlSchema.Read( new
          XmlTextReader(@ "c:\xmlxsdProbl em.xsd"), null);
          XmlDocument doc = new XmlDocument();
          doc.Schemas.Add (sch);
          doc.Load(@"c:\x mlxsdProblem.xm l");
          ValidationEvent Handler a = new
          ValidationEvent Handler(Validat ionhandler);
          doc.Validate(a) ;
          }
          >
          public void Validationhandl er(Object sender, ValidationEvent Args e)
          {
          //....
          }
          >
          Kevin Yu
          Microsoft Online Community Support
          =============== =============== =============== =====
          >
          (This posting is provided "AS IS", with no warranties, and confers no
          rights.)
          >
          >

          Comment

          • cduffy@ideacorporation.com

            #6
            Re: Upgrading XMLValidatingRe ader

            Hi Kevin,

            So the alternative is to load the load the entire document into ram?
            Is there really no stream based validator in 2.0?

            Chuck
            Kevin Yu [MSFT] wrote:
            Hi John,
            >
            The example in the document uses XmlReader.Creat e because it need to read
            from a reader. You can directly load from a file or something else. Here I
            wrote an example on my machine and hope it helps.
            >
            private void Form1_Load(obje ct sender, EventArgs e)
            {
            XmlSchema sch = XmlSchema.Read( new
            XmlTextReader(@ "c:\xmlxsdProbl em.xsd"), null);
            XmlDocument doc = new XmlDocument();
            doc.Schemas.Add (sch);
            doc.Load(@"c:\x mlxsdProblem.xm l");
            ValidationEvent Handler a = new
            ValidationEvent Handler(Validat ionhandler);
            doc.Validate(a) ;
            }
            >
            public void Validationhandl er(Object sender, ValidationEvent Args e)
            {
            //....
            }
            >
            Kevin Yu
            Microsoft Online Community Support
            =============== =============== =============== =====
            >
            (This posting is provided "AS IS", with no warranties, and confers no
            rights.)

            Comment

            • Christian Loris

              #7
              RE: Upgrading XMLValidatingRe ader

              Hi all,
              As you know, the XMLValidatingRe ader is deprecated in .NET 2.0. I have the
              following snippet from a .NET 1.1 implementation that I am struggling to
              upgrade. I know I should use XMLReader.Creat e, but am struggling to get much
              further. Any help would be appreciated. Thanks.
              >
              Public Shared Sub ValidateDocumen t(businessDocum ent As XmlDocument,
              schemaStrongNam e As String)
              ' Constants
              Const PARTS_IN_SCHEMA _STRONG_NAME As Integer = 2
              Const PART_CLASS_NAME As Integer = 0
              Const PART_QUALIFIED_ ASSEMBLY_NAME As Integer = 1
              >
              ' Parse schema strong name
              Dim assemblyNamePar ts As String() = schemaStrongNam e.Split(New Char()
              {","c}, PARTS_IN_SCHEMA _STRONG_NAME)
              Dim className As String = assemblyNamePar ts(PART_CLASS_N AME).Trim()
              Dim fullyQualifiedA ssemblyName As String =
              assemblyNamePar ts(PART_QUALIFI ED_ASSEMBLY_NAM E).Trim()
              >
              ' Load assembly
              Dim schemaAssembly As [Assembly] =
              [Assembly].Load(fullyQual ifiedAssemblyNa me)
              >
              ' Create instance of the BTS schema in order to get to the actual schemas
              Dim schemaType As Type = schemaAssembly. GetType(classNa me)
              Dim btsSchemaCollec tion As Microsoft.XLANG s.BaseTypes.Sch emaBase =
              CType(Activator .CreateInstance (schemaType),
              Microsoft.XLANG s.BaseTypes.Sch emaBase)
              >
              ' Set up XML validating reader and validate document
              Dim parserContext As New XmlParserContex t(Nothing, Nothing, "",
              XmlSpace.None)
              Dim reader As New XmlValidatingRe ader(businessDo cument.OuterXml ,
              XmlNodeType.Doc ument, parserContext)
              reader.Validati onType = ValidationType. Schema
              reader.Schemas. Add(btsSchemaCo llection.Schema Collection)
              While reader.Read()
              End While
              End Sub 'ValidateDocume nt
              >
              --
              John
              I just ran into the same problem you are having and here is the result of my conversion. You can read the article on my blog:

              Let me know if this is helpful!
              Christian Loris


              BizTalk Utilities - Frustration free BizTalk Adapters

              Comment

              Working...