Solved

Help with  validating specific xml file in a multiple folders using VB.NET

Posted on 2014-11-09
3
18 Views
Last Modified: 2016-06-23
Hi,

How do you modify the code below to validate xml files in multiple folders? For example I need to validate NSNBEL.XML in \Data\MasterNSN folder,  NSCBEL.xml in \Data\MasterNSN folder, but the .xsd files in the MasterFiles folder does not include the country, for example it contains NSN.xsd, NSC.xsd. Is there a way to but the folders in a string variable and use a loop instead of writing the same code for each folder and country. When I only one to validate for each country at a time not all countries in the folder.

 On Error GoTo tt
        Dim zz As String = ""
        For Each File As String In IO.Directory.GetFiles(Application.StartupPath + "\Data\MasterFuze\")
            Dim settings As New XmlReaderSettings
            settings.ValidationType = ValidationType.Schema

            settings.Schemas.Add(XmlSchema.Read(New XmlTextReader(Application.StartupPath + "\Data\MasterFiles\" & IO.Path.GetFileNameWithoutExtension(File) & ".xsd"), AddressOf ValidationHandler))

            Dim reader As XmlReader = XmlReader.Create(File, settings)
            zz = File

            Dim doc As New XmlDocument()
            doc.Load(reader)
        Next

        MsgBox("Master Files Validated")
        Exit Sub

Open in new window


Thanks,

Victor
0
Comment
Question by:vcharles
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 20

Accepted Solution

by:
ElrondCT earned 500 total points
ID: 40437709
Should the folder name in lines 3 and 7 be the same?

You could make this code into a function to which you pass the folder you want to validate:
    Private Function ValidateXML(ByVal CheckFolder As String) As Boolean
        On Error GoTo tt
        Dim zz As String = ""
        Dim File As String
        For Each File In IO.Directory.GetFiles(Application.StartupPath & CheckFolder)
            Dim settings As New XmlReaderSettings
            settings.ValidationType = ValidationType.Schema

            settings.Schemas.Add(XmlSchema.Read(New XmlTextReader(Application.StartupPath & CheckFolder & IO.Path.GetFileNameWithoutExtension(File) & ".xsd"), AddressOf ValidationHandler))

            Dim reader As XmlReader = XmlReader.Create(File, settings)
            zz = File

            Dim doc As New XmlDocument()
            doc.Load(reader)
        Next

        MsgBox("Master Files Validated")
        Return True

tt:
        MsgBox("Validation Error in " & File)
        Return False

    End Function

Open in new window


Then call it with each folder you want to check:
        blnValid = ValidateXML("\Data\MasterFiles\")
        blnValid = ValidateXML("\Data\MasterNSN\")

Open in new window

0
 

Author Comment

by:vcharles
ID: 40446951
Thank you for your solution, will test it and getback to you.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

697 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question