JoshStodola

Hi everyone,

I get an exception when I try to validate an XML stream against a provided schema. I am following another organizations standards with this, so I did not create the schema myself.

Exception: XmlSchemaValidationException

Message: The 'http://www.w3.org/XML/1998/namespace:lang' attribute is not declared.

I am not sure what this means, and I have been searching the web all day trying to figure it out. No luck, I am awaiting a response from the organization who created the schema, but they are very slow. I am thinking this might be something obvious, so if you have any ideas, please enlighten me.

Thanks in advance...




Re: XML and the .NET Framework Error when Validating against an XML Schema.

jkmyoung

http://www.w3.org/TR/REC-xml/#sec-lang-tag

The xml:lang attribute is used for specifying the language of the document, and should be valid.

By any chance are you using Xerces parser This is a known issue with that particular parser.





Re: XML and the .NET Framework Error when Validating against an XML Schema.

JoshStodola

Hi there, and thanks for the reply.

I am not using "Xerces". Are you saying that I need this attribute on every single tag or what Is it a problem with the schema or my XML

Not sure if this is relevant, but here is the code I use to validate the XML against a schema:

Imports System.IO
Imports System.Xml
Imports System.Xml.Schema
 
Public Class ValidateSchema
 
    Private ErrorList As ArrayList
    Private SchemaValidation As New ValidationEventHandler(AddressOf ValidationHandler)
 
    Public Sub New()
 
    End Sub
 
    Public Function ValidateXml(ByVal RawXml As String, ByVal SchemaFilePath As String) As ArrayList
       Dim Document As New XmlDocument()
       Dim blnReturn As Boolean = False
       ErrorList = New ArrayList()
 
       Try
           With Document
              .LoadXml(RawXml)
              .Schemas.Add(GetSchema(SchemaFilePath))
              .Validate(SchemaValidation)
           End With
 
           blnReturn = True
       Catch ex As Exception
           ErrorList.Add(ex.Message)
           Common.HandleException("ERROR in ValidateSchema.ValidateXml()", ex)
       End Try
 
       Return ErrorList
    End Function
 
    Private Function GetSchema(ByVal FilePath As String) As XmlSchema
       Dim Schema As XmlSchema
 
       Using Reader As New StreamReader(FilePath, Encoding.UTF8)
           Schema = XmlSchema.Read(Reader.BaseStream, SchemaValidation)
       End Using
 
       Return Schema
    End Function
 
    Private Sub ValidationHandler(ByVal sender As Object, ByVal e As System.Xml.Schema.ValidationEventArgs)
       ErrorList.Add(e.Message)
    End Sub
End Class

Thanks again!






Re: XML and the .NET Framework Error when Validating against an XML Schema.

Martin Honnen

You need to set the flag AllowXmlAttributes on your XmlReaderSettings e.g.

Code Snippet

XmlReaderSettings readerSettings = new XmlReaderSettings();

readerSettings.ValidationFlags |= XmlSchemaValidationFlags.AllowXmlAttributes;

Or you need to load a schema for those xml attributes. Setting the flag is easier and should suffice.






Re: XML and the .NET Framework Error when Validating against an XML Schema.

JoshStodola

Hi, and thanks for the reply.

I am not using any XmlReaderSettings. Am I doing this wrong or what !

Dear God, I hate XML.






Re: XML and the .NET Framework Error when Validating against an XML Schema.

Martin Honnen

You seem to use an XmlDocument just for validating your XML. That is not necessarily wrong but it is easier and less resource consuming if you simply use an XmlReader to do the validation. And that way you can apply XmlReaderSettings as suggested and set that flag which you can't do when using the Validate method of an XmlDocument. So I suggest to ditch that XmlDocument completely and simply use an XmlReader (over a StringReader if you have a string with XML) where you set up XmlReaderSettings as needed (ValidationType, ValidatationEventHandler) and that validation flag.

If you don't want to use that approach then you need to use

Code Snippet
Document.Schemas.Add(Nothing, "http://www.w3.org/2001/xml.xsd")

or rather use a local copy of that schema.






Re: XML and the .NET Framework Error when Validating against an XML Schema.

JoshStodola

Thanks Martin,

Do you know of any resources for doing what you described in VB.NET (using the XmlReader with settings)

Another thing, I tried to load a local copy of that schema as suggested, but the local schema had a doctype that points to a DTD like this:

<!DOCTYPE xsTongue Tiedchema PUBLIC "-//W3C//DTD XMLSCHEMA 200102//EN" "XMLSchema.dtd" >

And while debugging, it was looking for that DTD in this directory: C:\Program Files\Microsoft Visual Studio 8\Common7\IDE! So, I dont know what the heck to think about that. I have a copy of that DTD in the same folder as the schema but for some reason it is looking in that directory.

I am more clueless than ever before. I am going to give the XmlReader thing a shot but something tells me I will just end up at the same point I am at now.

Best regards...






Re: XML and the .NET Framework Error when Validating against an XML Schema.

JoshStodola

This fixed the problem, I no longer receive the error (while using the same schema validation class posted above):

Code Snippet

Document.Schemas.Add(Nothing, "http://www.w3.org/2001/xml.xsd")

However, the XML is not really getting validated. In other words, I never get any errors. I put some junk in the root element and it doesnt flag it as an error. Do you see any problems with my code

Thanks again!!






Re: XML and the .NET Framework Error when Validating against an XML Schema.

Martin Honnen

Please show us the start of your schema document and the XML sample you are trying to validate.






Re: XML and the .NET Framework Error when Validating against an XML Schema.

JoshStodola

Not sure how this is going to help, but here goes

< xml version="1.0" encoding="utf-8" >

<xsdTongue Tiedchema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.ACORD.org/standards/PC_Surety/ACORD1.12.0/xml/" elementFormDefault="unqualified" attributeFormDefault="unqualified" targetNamespace="http://www.ACORD.org/standards/PC_Surety/ACORD1.12.0/xml/" version="1.12.0">

<xsd:annotation xmlns="http://www.ACORD.org/standards/Support/xml/v1.0">

<xsd:appinfo>

<version>1.12.0</version>

<genDate>Tue Jun 12 18:44:04 EDT 2007</genDate>

</xsd:appinfo>

</xsd:annotation>

<xsd:import namespace="http://www.w3.org/XML/1998/namespace" schemaLocation="xml-ns.xsd">

<xsd:annotation>

<xsdBig Smileocumentation>Get access to the xml: attribute groups for xml:lang

</xsdBig Smileocumentation>

</xsd:annotation>

</xsd:import>

That is just a small chunk of the beginning of the schema. Both the schema and the XML are very very large. Here is a chunk of the XML I am trying to validate:

<ACORD>

<SignonRs>

<MsgStatus>

<MsgStatusCd>0</MsgStatusCd>

<MsgStatusDesc>Success</MsgStatusDesc>

</MsgStatus>

<CustId>

<SPName>com.appliedsystems</SPName>

<CustPermId />

<CustLoginId>REMOVED</CustLoginId>

</CustId>

<ClientDt>2007-08-10T12:18:36</ClientDt>

<CustLangPref>en-US</CustLangPref>

<ClientApp>

<Org>Applied Systems</Org>

<Name>WinTam</Name>

<Version>7.5.1</Version>

</ClientApp>

<ServerDt>2007-08-15T10:37:52.8440676-05:00</ServerDt>

<Language>en-US</Language>

</SignonRs>

Thanks Martin!!






Re: XML and the .NET Framework Error when Validating against an XML Schema.

Martin Honnen

You are running in the usual problem that your schema has a target namespace meaning it defines (at least) the root element in that namespace but your XML instance document has a root element ACORD in no namespace. Unfortunately schema validation in that case does not more than emit a warning that it does not find a matching schema for the root element and that warning is even only emitted when a certain flag to report warnings is set. Now the catch is that the API you are currently using (the Validate method of the XmlDocument) does not allow you to set those flags at all so in that case you do not get any messages.

As already suggested earlier it is possible to parse and validate with an XmlReader and XmlReaderSettings on which you can set those flags. The documentation at http://msdn2.microsoft.com/en-us/library/System.Xml.XmlReaderSettings.ValidationFlags.aspx has a VB.NET example, I hope it helps, it sets the flag to ReportValidationWarnings and that to ProcessInlineSchema, you only need to the former. And you can then set the flag to AllowXmlAttributes, that way you don't need to load the schema I pointed you to earlier. On the other hand your schema as posted above has an import for the XML namespace.

Lets us know whether you can adapt your code with the documentation sample at the link above.






Re: XML and the .NET Framework Error when Validating against an XML Schema.

JoshStodola

OK, i will give that a shot after my lunch hour.

Another question: I have two folders from this org. that contain the schemas and stuff. The folders are Schema_without_namespace and Schema_with_namespace. Which one would you recommend I use

Each folder has this file listing:

acord-doc.xsd

acord-pcs-v1_12_0-codes.xsd

acord-pcs-v1_12_0-nocodes.xsd

datatypes.dtd

xml-ns.xsd

xmlschema.dtd

I dont know what I need and what I dont, this is really confusing and I wish these idiots would provide me with some more information on how to use their schemas.

Thanks again for all your patience and help, I really do appreciate it!






Re: XML and the .NET Framework Error when Validating against an XML Schema.

JoshStodola

As expected, that MSDN code snippet is useless. Totally impractical and meaningless, doesnt even give me a place to start really. I need some sort of VB.NET XML guide on how to do this. I will try to shoot it in the dark, I guess that's how I did it with the XmlDocument.

There is no such thing as an XmlReader, but I found the XmlTextReader and it doesnt even have a constructor! I tried to use the Create() method but it doesnt accept a string value for the XML. I guess now I have to convert it to a Stream! God, what a pain in the ***.

Only in .NET can you shoot in the dark and hit a bullseye, so I will give it my best try.

Thanks Martin.






Re: XML and the .NET Framework Error when Validating against an XML Schema.

Martin Honnen

NET 2.0 and later has XmlReader with a factory method XmlReader.Create to create an XmlReader. As for parsing from a string I had already suggested to use an XmlReader over a StringReader.

I have tried to adapt the class you posted earlier to use XmlReaderSettings and XmlReader so that you hopefully find it more helpful than the MSDN sample. Here is the code (or rather what the forum software makes of it when pasting in stuff):

Code Snippet

Imports System.IO

Imports System.Xml

Imports System.Xml.Schema

Public Class ValidateSchema

Private ErrorList As ArrayList

Private _IsValid As Boolean

Public Property IsValid() As Boolean

Get

Return _IsValid

End Get

Set(ByVal value As Boolean)

_IsValid = value

End Set

End Property

Private SchemaValidation As New ValidationEventHandler(AddressOf ValidationHandler)

Public Sub New()

End Sub

Public Function ValidateXml(ByVal RawXml As String, ByVal SchemaFilePath As String) As ArrayList

ErrorList = New ArrayList()

IsValid = True

Dim ReaderSettings As XmlReaderSettings = New XmlReaderSettings()

ReaderSettings.ValidationType = ValidationType.Schema

ReaderSettings.ValidationFlags = ReaderSettings.ValidationFlags Or XmlSchemaValidationFlags.ReportValidationWarnings Or XmlSchemaValidationFlags.AllowXmlAttributes

ReaderSettings.Schemas.Add(Nothing, SchemaFilePath)

AddHandler ReaderSettings.ValidationEventHandler, SchemaValidation

Using Reader As XmlReader = XmlReader.Create(New StringReader(RawXml), ReaderSettings)

While Reader.Read()

End While

End Using

Return ErrorList

End Function

Private Sub ValidationHandler(ByVal sender As Object, ByVal e As System.Xml.Schema.ValidationEventArgs)

ErrorList.Add(e.Severity & ": " & e.Message)

If e.Severity = XmlSeverityType.Error Then

IsValid = False

End If

End Sub

End Class

'Use like this

Dim ExampleXml As String = "<foo xmlns=""http://example.com/2007/ex1""><bar>1</bar></foo>"

Dim Val As ValidateSchema = New ValidateSchema()

Dim Messages As ArrayList = Val.ValidateXml(ExampleXml, "..\..\XSDSchema1.xsd")

Console.WriteLine("Validation of {0} produced {1} message(s).", ExampleXml, Messages.Count)

For Each Message As String In Messages

Console.WriteLine(Message)

Next

Console.WriteLine("XML is valid: {0}.", Val.IsValid)

Console.WriteLine()

ExampleXml = "<foo xmlns=""http://example.com/2007/ex1""><bar>x</bar></foo>"

Messages = Val.ValidateXml(ExampleXml, "..\..\XSDSchema1.xsd")

Console.WriteLine("Validation of {0} produced {1} message(s).", ExampleXml, Messages.Count)

For Each Message As String In Messages

Console.WriteLine(Message)

Next

Console.WriteLine("XML is valid: {0}.", Val.IsValid)

Currently the IsValid property is set to false only when an error message is raised. Depending on your needs you might want to check for a warning too and set the property is false, for instance to cover the case that the root element namespace does not match the schema namespace.




Re: XML and the .NET Framework Error when Validating against an XML Schema.

JoshStodola

Thanks Martin!

Unfortunately, that just opened up a whole new can of worms. Now when it attempts to bring in the schema to the reader settings, it throws an exception saying that DTDs are prohibited and to set the ProhibitDtd property to false. I did that, but it still throws the exception becuase the settings are only applied when used in the Reader. I need it to allow DTDs. Any ideas

Sigh... did I mention that I hate XML

Thanks again, I am eagerly awaiting your response.