My questions are:
1. Why I am I getting this error?
2. How can I resolve?
I got the schema definition from a third party (ACORD).
I tried to do this using two different methods:
Method 1 – Using a LINQ XDoc (preferred way)
Private Function ValidXsd(ByVal AcordXml As String) As Boolean
Dim schemas As New XmlSchemaSet()
Dim returnVal As Boolean
schemas.Add(Nothing, XmlReader.Create(Server.MapPath("Includes
\acord-pcs-v1_15_1-ns-nodoc-codes.xsd")))
Dim xDocAcordXml = XDocument.Load(New StringReader(AcordXml))
xDocAcordXml.Validate(schemas, AddressOf
ValidationEventHandler, True)
If SchemaValidationResults.Count = 0 Then
returnVal = True
Else
'Populate AcordXml with values in SchemaValidationResults
End If
Return returnVal
End Function
Sub ValidationEventHandler(ByVal sender As Object, ByVal e As
System.Xml.Schema.ValidationEventArgs)
SchemaValidationResults.Add(e.Severity & ": " & e.Message)
End Sub
Method 2 – Using a XmlReader
Private Function ValidXsd2(ByVal AcordXml As String) As Boolean
Dim settings As New XmlReaderSettings()
Dim returnVal As Boolean
settings.Schemas.Add(Nothing, XmlReader.Create(Server.MapPath
("Includes\acord-pcs-v1_15_1-ns-nodoc-codes.xsd")))
settings.ValidationType = ValidationType.Schema
settings.ValidationFlags =
XmlSchemaValidationFlags.ReportValidationWarnings
AddHandler settings.ValidationEventHandler, AddressOf
settings_ValidationEventHandler
settings.IgnoreWhitespace = True
settings.IgnoreComments = True
Using reader As XmlReader = XmlReader.Create(New StringReader
(AcordXml), settings)
While (reader.Read())
'Empty loop
End While
End Using
If SchemaValidationResults.Count = 0 Then
returnVal = True
Else
'Populate AcordXml with values in SchemaValidationResults
End If
Return returnVal
End Function
' Display any warnings or errors.
Private Sub settings_ValidationEventHandler(ByVal sender As Object,
ByVal e As System.Xml.Schema.ValidationEventArgs)
SchemaValidationResults.Add(e.Severity & ": " & e.Message)
End Sub
I know the above code samples works since I tried them on a simple
example (XML, Schema). Plus I stole them from a book.
Any advice would be much appreciated.
Regards,
R
> Method 2 – Using a XmlReader
>
> Private Function ValidXsd2(ByVal AcordXml As String) As Boolean
>
> Dim settings As New XmlReaderSettings()
> Dim returnVal As Boolean
>
> settings.Schemas.Add(Nothing, XmlReader.Create(Server.MapPath
> ("Includes\acord-pcs-v1_15_1-ns-nodoc-codes.xsd")))
>
> settings.ValidationType = ValidationType.Schema
> settings.ValidationFlags =
> XmlSchemaValidationFlags.ReportValidationWarnings
You can set a flag here to allow xml: attributes like xml:lang or xml:space:
settings.ValidationFlags =
settings.ValidationFlags Or
XmlSchemaValidationFlags.AllowXmlAttributes
Or make your schema import a schema for the namespace
http://www.w3.org/XML/1998/namespace
<xsd:import namespace="http://www.w3.org/XML/1998/namespace"/>
that way I think it should work too.
On the other hand when I test with .NET 3.5 SP1 and try to validate an
XDocument having as follows:
XDocument doc = XDocument.Parse(@"<root xml:lang=""en""
xml:space=""preserve"">whatever</root>");
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.Add(null, @"..\..\XMLSchema1.xsd");
doc.Validate(schemaSet, delegate(object sender,
ValidationEventArgs vargs)
{
Console.WriteLine(vargs.Message);
}, true);
doc.Save(Console.Out);
where the schema is
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root" type="xs:string"/>
</xs:schema>
then I don't get any validation error so I am not sure why you get the
error. Does your schema import a schema for the namespace
http://www.w3.org/XML/1998/namespace which does not define the 'lang'
attribute?
Note that microsoft.public.dotnet.xml is a much better place to discuss
this.
--
Martin Honnen
http://msmvps.com/blogs/martin_honnen/
Hi Martin,
I got this to work with you help, however I just want to review what I
did just in case I misunderstood you.
I tried your first suggestion and I got the same error.
Here my code just in case I misunderstood you.
Private Function ValidXsd2(ByVal AcordXml As String) As Boolean
Dim settings As New XmlReaderSettings()
Dim returnVal As Boolean
settings.Schemas.Add(Nothing, XmlReader.Create(Server.MapPath
("Includes\acord-pcs-v1_15_1-ns-nodoc-codes.xsd")))
settings.ValidationType = ValidationType.Schema
settings.ValidationFlags =
XmlSchemaValidationFlags.ReportValidationWarnings
settings.ValidationFlags = settings.ValidationFlags Or
XmlSchemaValidationFlags.AllowXmlAttributes
AddHandler settings.ValidationEventHandler, AddressOf
settings_ValidationEventHandler
settings.IgnoreWhitespace = True
settings.IgnoreComments = True
Using reader As XmlReader = XmlReader.Create(New StringReader
(AcordXml), settings)
While (reader.Read())
'Empty loop
End While
End Using
If SchemaValidationResults.Count = 0 Then
returnVal = True
Else
'Populate AcordXml with values in SchemaValidationResults
End If
Return returnVal
End Function
I tried your second suggestion.
This line was currently in the schema:
<xsd:import namespace="http://www.w3.org/XML/1998/namespace"
schemaLocation="xml-ns.xsd">
So, I changed it like you suggested to the following:
<xsd:import namespace="http://www.w3.org/XML/1998/namespace">
After I made this change, the processing went into the validation
processing.
Let me know if I misinterpreted any of you suggestions.
Regards
PS Thanks for you help
If you get the same error with that suggestion then it seems as if your
schema imports a schema for the namespace
http://www.w3.org/XML/1998/namespace where the imported schema does not
define the 'lang' attribute.
> I tried your second suggestion.
>
> This line was currently in the schema:
> <xsd:import namespace="http://www.w3.org/XML/1998/namespace"
> schemaLocation="xml-ns.xsd">
>
> So, I changed it like you suggested to the following:
>
> <xsd:import namespace="http://www.w3.org/XML/1998/namespace">
>
> After I made this change, the processing went into the validation
> processing.
So that indeed shows that your main schema already imported a schema for
the namespace http://www.w3.org/XML/1998/namespace. That means you got
the error because xml-ns.xsd did not define the 'lang' attribute. Have
you checked the schema xml-ns.xsd? Does that exist in the same location
as the importing schema? Can you post how it looks?
As for the change now get you what you want, yes, if you only do
<xsd:import namespace="http://www.w3.org/XML/1998/namespace">
without providing a schema location then the .NET implementation uses a
default schema for that namespace that defines attributes like xml:lang
or xml:space. So that is one way to get what you want. You could also
remove the xsd:import completely and set the flag AllowXmlAttributes, at
least as long as you use XmlReaderSettings and XmlReader.
Here is code to resolve this issue just in case anyone stumbles on
this post:
(Thanks for your help Martin)
Private SchemaValidationResults As New List(Of String)
Private Function ValidXsd(ByVal AcordXml As String) As Boolean
Dim schemas As New XmlSchemaSet()
Dim returnVal As Boolean
Dim settings As New XmlReaderSettings()
settings.ProhibitDtd = False
schemas.Add(Nothing, XmlReader.Create(Server.MapPath("Includes
\acord-pcs-v1_15_1-ns-nodoc-codes.xsd")))
schemas.Add(Nothing, XmlReader.Create(Server.MapPath("Includes
\xml-ns.xsd"), settings))
Dim xDocAcordXml = XDocument.Load(New StringReader(AcordXml))
xDocAcordXml.Validate(schemas, AddressOf
ValidationEventHandler, True)
If SchemaValidationResults.Count = 0 Then
returnVal = True
Else
'Populate AcordXml with values in SchemaValidationResults
returnVal = False
End If
Return returnVal
End Function
Sub ValidationEventHandler(ByVal sender As Object, ByVal e As