Click here to Skip to main content
15,887,776 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: How to Set Allow Zero Length for Field using OLEDBCommand Pin
Matt U.11-Nov-14 8:13
Matt U.11-Nov-14 8:13 
GeneralRe: How to Set Allow Zero Length for Field using OLEDBCommand Pin
gwittlock11-Nov-14 8:16
gwittlock11-Nov-14 8:16 
GeneralRe: How to Set Allow Zero Length for Field using OLEDBCommand Pin
Eddy Vluggen11-Nov-14 8:25
professionalEddy Vluggen11-Nov-14 8:25 
GeneralRe: How to Set Allow Zero Length for Field using OLEDBCommand Pin
gwittlock11-Nov-14 8:42
gwittlock11-Nov-14 8:42 
GeneralRe: How to Set Allow Zero Length for Field using OLEDBCommand Pin
Eddy Vluggen11-Nov-14 11:51
professionalEddy Vluggen11-Nov-14 11:51 
GeneralRe: How to Set Allow Zero Length for Field using OLEDBCommand Pin
gwittlock11-Nov-14 13:11
gwittlock11-Nov-14 13:11 
GeneralRe: How to Set Allow Zero Length for Field using OLEDBCommand Pin
Eddy Vluggen12-Nov-14 8:00
professionalEddy Vluggen12-Nov-14 8:00 
QuestionXmlReader: {none} - modified Pin
Maciej Los11-Nov-14 3:27
mveMaciej Los11-Nov-14 3:27 
I wrote simple xml validator using XmlDocument.Validate Method (ValidationEventHandler)[^], but it isn't working as expected. My validator returns zero errors even if the xml structure does not corresponds to schema!!!

Additional information:
Compiler does not show any errors and warnings.
Code analysis does not display any issues.
The files are located in two different path:
C#
xml = %userdocs%\MyProgramm\XML\xmlfile.xml
xsd = %userdocs%\MyProgramm\Schema\schemafile.xsd


During debug the programme, the debugger shows in this line
VB
Dim reader As XmlReader = XmlReader.Create("contosoBooks.xml", settings)

XmlReader: {nothing} (as expected!)
and when i step into the next line
XmlReader: {none}.

Could it be the reason of issue? As per i understand MSDN documentation[^], Create method should return XmlReader object. What is going on?

[EDIT]
xml:
XML
<?xml version="1.0"?>
<MyTool xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Settings>
    <AdReqs/>
  </Settings>
</MyTool>


xsd:
XML
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:MyTool">
  <xs:element name="Settings">
    <xs:complexType>
      <xs:sequence>
        <xs:element name ="AddReqs" default="Y">
          <xs:simpleType>
            <xs:restriction base="xs:string">
              <xs:enumeration value="Y"/>
              <xs:enumeration value="N"/>
            </xs:restriction>
          </xs:simpleType>
        </xs:element >
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>


Code snippet (of TXmlValidator class):
VB
Imports System.Xml.Schema
Imports System.Xml.XPath
Imports System.Text

Public Class TXmlValidator
    Implements IXmlValidator

    Private vErrLst As List(Of String) = New List(Of String)

    Sub ValidateXml(ByVal xmlFile As String, ByVal xsdFile As String) Implements IXmlValidator.ValidateXml

        Try
            Dim settings As Xml.XmlReaderSettings = New Xml.XmlReaderSettings()
            settings.Schemas.Add("urn:MyTool", xsdFile)
            settings.ValidationType = Xml.ValidationType.Schema

            Dim reader As Xml.XmlReader = Xml.XmlReader.Create(xmlFile, settings)

            Dim document As Xml.XmlDocument = New Xml.XmlDocument()
            document.Load(reader)

            Dim eventHandler As ValidationEventHandler = New ValidationEventHandler(AddressOf ValidationEventHandler)

            ' the following call to Validate succeeds.
            document.Validate(eventHandler)

            reader.Close()
            reader = Nothing
            document = Nothing

        Catch ex As Exception
            MsgBox(ex.Message)

        End Try
    End Sub

    Sub ValidateXml1(ByVal xmlFile As String, ByVal xsdFile As String) Implements IXmlValidator.ValidateXml1
        Try
            Dim xsdMarkup As XElement = XElement.Load(xsdFile)
            Dim schemas As XmlSchemaSet = New XmlSchemaSet()
            schemas.Add("urn:MyTool", xsdMarkup.CreateReader)
            Dim document As XDocument = XDocument.Load(xmlFile)

            ' the following call to Validate succeeds.
            document.Validate(schemas, AddressOf ValidationEventHandler)
            document = Nothing
        Catch ex As Exception
            MsgBox(ex.Message)
        
        End Try
    End Sub


    Public ReadOnly Property GetErrorList() As List(Of String) Implements IXmlValidator.GetErrorList

        Get
            Return vErrLst
        End Get

    End Property

    Private Sub ValidationEventHandler(ByVal sender As Object, ByVal e As ValidationEventArgs)

        Select Case e.Severity
            Case XmlSeverityType.Error
                vErrLst.Add(String.Format("Error in line: {0} = {1}", e.Exception.LineNumber, e.Message))
            Case XmlSeverityType.Warning
                vErrLst.Add(String.Format("Warning in line: {0} = {1}", e.Exception.LineNumber, e.Message))
        End Select
        'fw.Close()
        'End Using

    End Sub

End Class

Above code is called in WinForm this way:
VB
Dim xv As IXmlValidator = New TXmlValidator()
xv.ValidateXml(Me.TxtXml.Text, Me.TxtXsd.Text)
Dim qry As List(Of String) = xv.GetErrorList()
Dim iErr As Integer = qry.Count()

Me.LstOfErrors.Items.Add(String.Format("Errors count: {0} - document is {1} valid!", iErr.ToString(), IIf(iErr > 0, "not", "")))

xv.ValidateXml1(Me.TxtXml.Text, Me.TxtXsd.Text)
qry = xv.GetErrorList()
iErr = qry.Count()
Me.LstOfErrors.Items.Add(String.Format("Errors count: {0} - document is {1} valid!", iErr.ToString(), IIf(iErr > 0, "not", "")))

xv = Nothing

Note: above code was written in hurry. Real code is a part of biggest project. I can't post it.

As you can see AddReqs must be one these values: "Y" or "N" only. In an xml file Addreqs field is empty!
GeneralRe: XmlReader: {none} Pin
PIEBALDconsult11-Nov-14 3:50
mvePIEBALDconsult11-Nov-14 3:50 
GeneralRe: XmlReader: {none} Pin
Maciej Los11-Nov-14 4:45
mveMaciej Los11-Nov-14 4:45 
GeneralRe: XmlReader: {none} Pin
vinuvasahanponniah11-Nov-14 20:04
professionalvinuvasahanponniah11-Nov-14 20:04 
GeneralRe: XmlReader: {none} Pin
Maciej Los12-Nov-14 9:17
mveMaciej Los12-Nov-14 9:17 
Questionhow to convert pdf file into tif file in Visual Basic 2008 code Pin
Virendra Singh Bhanu9-Nov-14 1:44
Virendra Singh Bhanu9-Nov-14 1:44 
AnswerRe: how to convert pdf file into tif file in Visual Basic 2008 code Pin
Richard MacCutchan9-Nov-14 2:30
mveRichard MacCutchan9-Nov-14 2:30 
AnswerRe: how to convert pdf file into tif file in Visual Basic 2008 code Pin
Eddy Vluggen9-Nov-14 5:56
professionalEddy Vluggen9-Nov-14 5:56 
QuestionSending email through VB6 Pin
Member 104103238-Nov-14 1:54
professionalMember 104103238-Nov-14 1:54 
SuggestionRe: Sending email through VB6 Pin
Richard MacCutchan8-Nov-14 2:46
mveRichard MacCutchan8-Nov-14 2:46 
AnswerRe: Sending email through VB6 Pin
Mycroft Holmes8-Nov-14 12:28
professionalMycroft Holmes8-Nov-14 12:28 
GeneralRe: Sending email through VB6 Pin
ACIVE11-Nov-14 13:24
ACIVE11-Nov-14 13:24 
GeneralRe: Sending email through VB6 Pin
Mycroft Holmes11-Nov-14 13:42
professionalMycroft Holmes11-Nov-14 13:42 
GeneralRe: Sending email through VB6 Pin
ACIVE12-Nov-14 0:39
ACIVE12-Nov-14 0:39 
Question1073479673(c0040007) Pin
Krishna Kamal7-Nov-14 18:44
professionalKrishna Kamal7-Nov-14 18:44 
AnswerRe: 1073479673(c0040007) Pin
Richard MacCutchan7-Nov-14 22:05
mveRichard MacCutchan7-Nov-14 22:05 
GeneralRe: 1073479673(c0040007) Pin
Krishna Kamal10-Nov-14 22:17
professionalKrishna Kamal10-Nov-14 22:17 
GeneralRe: 1073479673(c0040007) Pin
Richard MacCutchan10-Nov-14 23:11
mveRichard MacCutchan10-Nov-14 23:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.