Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries >Thread Prev - Re: BUG: The minLength attribute in an XML Schema is not validated [Thread Next] Re: BUG: The minLength attribute in an XML Schema is not validatedTo: NULL Date: 10/30/2008 9:12:00 AM Looking a little further into this I am not sure that IgnoreWhitespace is the
solution I want. Is there any other way to go from an XmlDocument object
to a XmlReader used for validation? I am trying to avoid writing a file to
disk as a part of this process.
"TimB" wrote:
> Thank you. I am doing wc3 validation though a little differently than your
> sample.
> Below is some VB.Net code I used to test with. "Try1" is using your method
> as close as I could translate it to VB. "Try1" has the same results as your
> test. "Try2" is how I was doing validation. I start with a loaded
> XmlDocument (received via messaging) rather than from a file in the file
> system. To get the XML into the XmlReader I first save it to a memory
> stream and then create the XmlReader with the content of the memory stream.
> "Try2" does not pick up the validation error on the foo element. "Try3" is
> using my method but with the IgnoreWhitespace XMLReaderSetting set to true.
> "Try3" does pick up both validation errors. Something about saving the
> XmlDocument to a memory stream and then creating the XmlReader from that
> stream must change the white space in the document.
>
> This seems to have changed at the time I installed the .Net Framework
> version 3.5 (Sp1). I mistakenly believed the problem was caused by the bug
> identified in article 826753. Knowing that wasn't the case helped a great
> deal!
>
>
> Sub main()
> Dim objXMLDoc As XmlDocument
>
> try1("c:\temp\xmlfile1.xml")
>
> objXMLDoc = New XmlDocument
> objXMLDoc.Load("c:\temp\xmlfile1.xml")
> try2(objXMLDoc)
>
>
> objXMLDoc = New XmlDocument
> objXMLDoc.Load("c:\temp\xmlfile1.xml")
> try3(objXMLDoc)
>
> End Sub
>
> Sub try1(ByVal astrFileName As String)
> 'Validate an xml document. Pull the document from the file system.
>
> System.Console.WriteLine("Try1")
> Dim objXMLReaderSettigns As XmlReaderSettings = New XmlReaderSettings
> objXMLReaderSettigns.ValidationType = ValidationType.Schema
> objXMLReaderSettigns.Schemas.Add(Nothing, "c:\temp\xmlschema1.xsd")
> AddHandler objXMLReaderSettigns.ValidationEventHandler, AddressOf
> ValidationCallBack
>
> Dim objXMLReader As XmlReader = XmlReader.Create(astrFileName,
> objXMLReaderSettigns)
>
> While objXMLReader.Read
> End While
>
> End Sub
>
> Sub try2(ByVal aobjXMLDoc As XmlDocument)
> 'Validate an xml document. XML Document is alread loaded into an
> XmlDocument object.
>
> System.Console.WriteLine("Try2")
> Dim objXMLReaderSettigns As XmlReaderSettings = New XmlReaderSettings
> objXMLReaderSettigns.ValidationType = ValidationType.Schema
> objXMLReaderSettigns.Schemas.Add(Nothing, "c:\temp\xmlschema1.xsd")
> AddHandler objXMLReaderSettigns.ValidationEventHandler, AddressOf
> ValidationCallBack
>
> Dim objMemoryStrm As System.IO.Stream = New System.IO.MemoryStream
> aobjXMLDoc.Save(objMemoryStrm)
> objMemoryStrm.Position = 0
> Dim objTextReader As XmlTextReader = New XmlTextReader(objMemoryStrm)
>
> Dim objXMLReader As XmlReader = XmlReader.Create(objTextReader,
> objXMLReaderSettigns)
>
> While objXMLReader.Read
> End While
>
> End Sub
>
> Sub try3(ByVal aobjXMLDoc As XmlDocument)
> 'Validate an xml document. XML Document is alread loaded into an
> XmlDocument object.
>
> System.Console.WriteLine("Try3")
> Dim objXMLReaderSettigns As XmlReaderSettings = New XmlReaderSettings
> objXMLReaderSettigns.ValidationType = ValidationType.Schema
> objXMLReaderSettigns.IgnoreWhitespace = True
> objXMLReaderSettigns.Schemas.Add(Nothing, "c:\temp\xmlschema1.xsd")
> AddHandler objXMLReaderSettigns.ValidationEventHandler, AddressOf
> ValidationCallBack
>
> Dim objMemoryStrm As System.IO.MemoryStream = New
> System.IO.MemoryStream
> aobjXMLDoc.Save(objMemoryStrm)
> objMemoryStrm.Position = 0
> Dim objTextReader As XmlTextReader = New XmlTextReader(objMemoryStrm)
>
> Dim objXMLReader As XmlReader = XmlReader.Create(objTextReader,
> objXMLReaderSettigns)
>
> While objXMLReader.Read
> End While
>
> End Sub
>
> Private Sub ValidationCallBack(ByVal sender As Object, ByVal args As _
> ValidationEventArgs)
> 'Display the validation error. This is only called on error
> System.Console.WriteLine("Validation error: " + args.Message)
> End Sub
>
>
> The following are the results from all 3 tries:
>
> Try1
> Validation error: The 'foo' element is invalid - The value '' is invalid
> according to its datatype 'String' - The actual length is less than the
> MinLength value.
> Validation error: The 'baz' attribute is invalid - The value '' is invalid
> according to its datatype 'String' - The actual length is less than the
> MinLength value.
> Try2
> Validation error: The 'baz' attribute is invalid - The value '' is invalid
> according to its datatype 'String' - The actual length is less than the
> MinLength value.
> Try3
> Validation error: The 'foo' element is invalid - The value '' is invalid
> according to its datatype 'String' - The actual length is less than the
> MinLength value.
> Validation error: The 'baz' attribute is invalid - The value '' is invalid
> according to its datatype 'String' - The actual length is less than the
> MinLength value.
>
>
>
> "Martin Honnen" wrote:
>
> > TimB wrote:
> > > Does anyone know when the issue identified in the following knowledge base
> > > article will be fixed with a service pack to the .Net Framework version 3.5?
> > >
> > > http://support.microsoft.com/default.aspx/kb/826753
> > >
> > > This issue prevents empty elements or attributes that have a minLength
> > > greater than zero from being identified as validation errors when validating
> > > an xml document using a schema.
> >
> > That problem linked to above applies to
> >
> > • Microsoft XML Core Services 4.0
> > • Microsoft XML Core Services 4.0 Service Pack 1
> > • Microsoft XML Parser 3.0 Service Pack 3
> > • Microsoft XML Parser 3.0 Service Pack 2
> > • Microsoft XML Parser 3.0 Service Pack 1
> > • Microsoft XML Parser 3.0
> > • Microsoft XML Parser 2.6
> > • Microsoft XML Parser 2.5
> > • Microsoft XML Parser 2.0
> >
> > and not to the .NET framework.
> >
> > With the .NET framework and a W3C XML schema the problem does not exist:
> >
> > XmlReaderSettings xrs = new XmlReaderSettings();
> > xrs.ValidationType = ValidationType.Schema;
> > xrs.ValidationEventHandler += delegate(object sender,
> > ValidationEventArgs vargs)
> > {
> > Console.WriteLine("{0}: {1}", vargs.Severity,
> > vargs.Message);
> > };
> > xrs.Schemas.Add(null, @"..\..\XMLSchema1.xsd");
> >
> > using (XmlReader rd =
> > XmlReader.Create(@"..\..\XMLFile1.xml", xrs))
> > {
> > while (rd.Read()) { }
> > }
> >
> > where the schema is as follows:
> > <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
> > <xs:element name="root">
> > <xs:complexType>
> > <xs:sequence>
> > <xs:element name="foo">
> > <xs:simpleType>
> > <xs:restriction base="xs:string">
> > <xs:minLength value="2"/>
> > </xs:restriction>
> > </xs:simpleType>
> > </xs:element>
> > <xs:element name="bar">
> > <xs:complexType>
> > <xs:attribute name="baz" use="required">
> > <xs:simpleType>
> > <xs:restriction base="xs:string">
> > <xs:minLength value="2"/>
> > </xs:restriction>
> > </xs:simpleType>
> > </xs:attribute>
> > </xs:complexType>
> > </xs:element>
> > </xs:sequence>
> > </xs:complexType>
> > </xs:element>
> > </xs:schema>
> >
> > and the instance is as follows:
> > <?xml version="1.0" encoding="utf-8" ?>
> > <root>
> > <foo></foo>
> > <bar baz=""/>
> > </root>
> >
> >
> > outputs the following two validation errors with Visual Studio 2005:
> >
> > Error: The 'foo' element is invalid - The value '' is invalid according
> > to its datatype 'String' - The actual length is less than the MinLength
> > value.
> > Error: The 'baz' attribute is invalid - The value '' is invalid
> > according to its datatype 'String' - The actual length is less than the
> > MinLength value.
> >
> >
> > Or are you still using an XDR schema?
> >
> > --
> >
> > Martin Honnen --- MVP XML
> > http://JavaScript.FAQTs.com/
> >
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
