Expand|Select|Wrap|Line Numbers
- Imports Microsoft.VisualBasic
- Imports System.Xml
- Imports System.Xml.Schema
- Public Class DataValidator
- Const SCHEMA_SUFFIX As String = "XMLSchema.xsd"
- Const SCHEMA_NAMESP As String = "activationSchema"
- Private Failed As String
- Public Function ValidateXML(ByVal xmlData As String, ByVal custId As String) As String
- Failed = ""
- ' Create the XmlSchemaSet class.
- Dim sc As XmlSchemaSet = New XmlSchemaSet()
- ' Add the schema to the collection.
- sc.Add(SCHEMA_NAMESP, "http://localhost/ActivationData/" & custId & SCHEMA_SUFFIX)
- ' Create and load the XML document
- Dim doc As XmlDocument = New XmlDocument()
- doc.LoadXml(xmlData)
- ' Create an XmlNodeReader using the XML document
- Dim nodeReader As XmlNodeReader = New XmlNodeReader(doc)
- ' Set the validation settings.
- Dim settings As XmlReaderSettings = New XmlReaderSettings()
- settings.ValidationType = ValidationType.Schema
- settings.ValidationFlags = settings.ValidationFlags _
- Or XmlSchemaValidationFlags.ReportValidationWarnings Or XmlSchemaValidationFlags.AllowXmlAttributes
- settings.Schemas = sc
- AddHandler settings.ValidationEventHandler, AddressOf ValidationCallBack
- ' Create the XmlReader object.
- Dim reader As XmlReader = XmlReader.Create(nodeReader, settings)
- ' Parse the file.
- While reader.Read()
- End While
- Return Failed
- End Function
- ' Compile a string of any validation errors
- Private Sub ValidationCallBack(ByVal sender As Object, ByVal e As ValidationEventArgs)
- Failed = Failed & "XML Validation Error: {0}" & e.Message & vbCrLf
- End Sub
- End Class
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema id="C10008XMLSchema"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="activationSchema" elementFormDefault="qualified" targetNamespace="activationSchema">
<xsd:element name="activationData">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="custPO" type="xsd:string" minOccurs="1" />
<xsd:element name="itemCode" type="xsd:string" minOccurs="1" />
<xsd:element name="esn" type="xsd:string" minOccurs="1" />
<xsd:element name="npa" type="xsd:string" minOccurs="1" />
<xsd:element name="mdn" type="xsd:string" minOccurs="1" />
<xsd:element name="msid" type="xsd:string" minOccurs="1" />
<xsd:element name="activationCode" type="xsd:string" minOccurs="1" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
My XML:
<activationData>
<custPO>1421</custPO>
<itemCode>C2125---C1819DGNI-00</itemCode>
<esn>03709911440</esn>
<npa>444</npa>
<mdn>mdn1212</mdn>
<msid>msid1212</msid>
<activationCode>650</activationCode>
</activationData>
When I pass the above xml into the service, I get this error:
"Could not find schema information for the element 'activationData'."
Questions:
1. The element exists in the xml - why the error?
2. In the cs.add statement, how can I get around having to hardcode the URL? Is this how you tell the process where the xsd is?
3. Am I even doing this right? Should I use the nodeReader?
Thank you!