?
Solved

URGENT - parsing xml documents in VB.Net

Posted on 2005-04-27
9
Medium Priority
?
184 Views
Last Modified: 2010-04-23
I am writing my first class library (.dll) which will need to extract certain data from an xml document.

I have template code from a classic ASP application I can use as a guide to getting the data out, based
on the old XMLDOM object. This is what I have so far:

==================================
Namespace xmlConverter

    Public Class A1Extractor

        Private xmlFile As String
        Private isValid As Boolean
_________________________________________________

        Public Property inputFile() As String
            Get
                Return xmlFile
            End Get
            Set(ByVal Value As String)
                inputFile = Value
            End Set
        End Property
_________________________________________________

        Public Sub New()
            Dim rdrXML As XmlTextReader = New XmlTextReader(xmlFile)
            Dim rdrVal As New XmlValidatingReader(rdrXML)
            rdrVal.ValidationType = ValidationType.DTD
            'create event handler for events raised by validator
            AddHandler rdrVal.ValidationEventHandler, AddressOf valEvtHandler
            ' Check whether the document is valid or invalid.
            If isValid Then
                'if successful, perform extraction and write A1 file to disk.
                extractA1(xmlFile)
                'if writing to disk is successful, show msgbox with new file location in parent app
            End If
        End Sub
_________________________________________________

        Public Sub valEvtHandler(ByVal sender As Object, ByVal args As ValidationEventArgs)
            isValid = False
            MsgBox("This document is invalid. The error returned was: " & vbCrLf & args.Message)
        End Sub
_________________________________________________

        Public Sub extractA1(byval xmlFile as string)
            While rdrVal.Read()
                ' add processing instructions here
            End While
            rdrVal.Close()
        End Sub

    End Class

End Namespace
====================================

QUESTIONS

1) Have I organized the logic correctly?
2) How can I make the event handler above return an error message to the calling application
    rather than trying to create its own msgBox? Do I need to write a function / method that
    returns the error and exits the .dll? How?
3) Is there anything else I am missing for the purpose of making this .dll able to be called
    from different programs?

Thanks

0
Comment
Question by:k3n51mm
  • 5
  • 4
9 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 13877352
You might be able to use XPath queries.  What does the format of the XML look like?

Bob
0
 
LVL 1

Author Comment

by:k3n51mm
ID: 13877468
Thanks for the response Bob, but my questions were not about how to get the data out of the XML document - I have existing code in VBScript that I can port to .Net. The real issues were listed at the bottom of the code sample.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 13877570
Ok, with that out of the way, think about these suggestions:

(1) Add a ValidationError event:

Public Event ValidationError(sender As Object, e As ValidationEventArgs)

(2) Change the New constructor to add the file path to the constructor:

Public Sub New(ByVal xmlFile As String)

    Dim rdrXML As XmlTextReader = New XmlTextReader(xmlFile)
    Dim rdrVal As New XmlValidatingReader(rdrXML)

    rdrVal.ValidationType = ValidationType.DTD

    'create event handler for events raised by validator
    AddHandler rdrVal.ValidationEventHandler, AddressOf valEvtHandler

    ' Check whether the document is valid or invalid.
    If isValid Then
   
         'if successful, perform extraction and write A1 file to disk.
         extractA1(xmlFile)

     End If

 End Sub

(3)  Modify the validation event handler.

          Public Sub valEvtHandler(ByVal sender As Object, ByVal args As ValidationEventArgs)

             isValid = False

             ' pass the validation error on to the caller.
             RaiseEvent ValidationError(sender, args)

           End Sub

Bob
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:k3n51mm
ID: 13877747

Excellent. I have to learn about how to raise events properly, and I'll be using your method in several places.

It must be so Zen to actually know what the hell you're doing...I envy your knowledge.  But I'm working on it!

Thanks Bob.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 13877773
Cool place to learn:

VB at the Movies:
http://msdn.microsoft.com/vbasic/atthemovies/

Bob
0
 
LVL 1

Author Comment

by:k3n51mm
ID: 13877912
Hehe, I'm there.

Oh yeah, one more thing - I had set the input file string as a property, and I noticed you suggested I pass it as a parameter. Why?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 13877994
When instantiating a new class, the New constructor is called before the property is set, so xmlFile is going to be uninitialized.  Since strings are objects, you will get an 'Object or with block not set' error.

This brings up another thing that I try to always do:
Private xmlFile As String = String.Empty

Otherwise, xmlFile is Nothing.

Another thing that I have learned over time is that properties can be lost in all the properties in the AutoComplete window, but when you pass the value as a parameter in the constructor, then you are forced to specify it, or you'll get an error.  If you define a class without a default constructor (New without any parameters), then you are forced to pass the file name as a parameter, or you'll get a compile error about not enough arguments when you try to instantiate a new object from the class.

Bob

0
 
LVL 1

Author Comment

by:k3n51mm
ID: 13878084

...Hence the user name. Thanks again.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 13878141
Anytime :)

Bob
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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Integration Management Part 2
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

750 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