Solved

Find XML value quickly- without too much overhead and fuss.

Posted on 2004-05-02
5
926 Views
Last Modified: 2012-06-27
This is part of my XML file:-
<?xml version="1.0" ?>
- <Timeline Name="50144595" Path="E:\source\Server\Timelines\50144595.tml" Checksum="239820" ProclaimVersion="1.5" BitmapItems="2" VideoItems="1" InfoboxItems="1" ScrollItems="1">

I am using it as a INI which will be accessed every few minutes for certain values.  However, I have been studying Xpath and few other ways, but what I really want is a simple VB.NET function like we used to do when reading INI files in VB6.

I was to check the "Checksum" value without going through a collection, I would like to call it like value=GetChecksumFromXML(filename)

It it is ciritical that I can do this quickly without too much processor activity as it is part of a smooth running display.  Just a quick grab, but without using collections any other iterator type routines.

Regards
Dave
Posted Sunday Afternoon in UK.
0
Comment
Question by:DaveRowland
  • 3
  • 2
5 Comments
 
LVL 1

Author Comment

by:DaveRowland
ID: 10971273
Anyone?  appreciate a quick answer.. increased points
0
 
LVL 6

Expert Comment

by:danths
ID: 10972665
Did you consider putting your entry in app.config?
and then http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpgrfaccessingappsettingssection.asp

Internally they will have to go thru an iterator,  however the interface will be easier for you.

0
 
LVL 1

Author Comment

by:DaveRowland
ID: 10973033
nope.. no good.  I refer to INI but they are not app settings, they are changing values by external programs for media purposes.. dont ask.  But I am currently implementing XPath method, but would still like to be able to address "directly" a XML file.

In other words,  

1:  Find "Timeline/Checksum" in fred.xml
2:  Returns 239820 to the function that called it.

I don't want to Load XML file, Prepare pointers etc., then "do loop" it to find Checksum and then return a value back.  So much overhead, must be a way of finding a value quickly.
0
 
LVL 6

Accepted Solution

by:
danths earned 500 total points
ID: 10973672
Not sure what your objective is? You would want to use an XML file for setting but assume that parsing an XML file is an overhead. What type of parser are you planning to use, a DOM or SAX. Did you go thru the basics of understanding what they are and when and how to use them? How and why did you discard them as overhead. Did you do some profiling of the code?

Lets completely forget about using XML for such things? You consider getting an value in probably VB6 INI would be fast enough, if so would you really put such dynamic values in an INI? If not why are you considering an XML for this?
Any how I think you should revise what and why you are doing? I am still posting a function that I often use for a DOM based ( XMLDcoument) parsing


Protected Function GetElementValue(ByVal xmlDoc As XmlDocument, _
                                            ByVal elementPattern As String) As Collection
            Dim oXMLNode As XmlNode
            Dim oXMlAttr As XmlAttribute
            Dim oXMLNodeList As XmlNodeList
            Dim colValue As New Collection
            Dim iCounter As Integer
            Dim strValue As String


            'Initialize the iCounter to 1
            iCounter = 1

            Try
                oXMLNodeList = xmlDoc.SelectNodes(elementPattern)
            Catch e As Exception
                If mLogger.IsFatalEnabled Then mLogger.Fatal(e.ToString)
            End Try

            If (oXMLNodeList.Count > 0) Then
                For Each oXMLNode In oXMLNodeList
                    iCounter = 1
                    If (oXMLNode.NodeType = XmlNodeType.Element And oXMLNode.HasChildNodes) Then

                        strValue = oXMLNode.ChildNodes.Item(iCounter - 1).Value

                        If Not (strValue Is Nothing) Then
                            colValue.Add(oXMLNode.ChildNodes.Item(iCounter - 1).Value)
                        End If

                    End If
                    strValue = Nothing
                Next
            End If

            GetElementValue = colValue

        End Function

        Protected Function GetAttributeValue(ByVal xmlDoc As XmlDocument, _
                                            ByVal elementPattern As String, _
                                            ByVal vAttrPattern As String) As Collection
            Dim oXMLNode As XmlNode
            Dim oXMlAttr As XmlAttribute
            Dim oXMLNodeList As XmlNodeList
            Dim colValue As New Collection
            Dim iCounter As Integer
            Dim strAttrValue As String

            iCounter = 1
            'This should return only one value
            Try
                oXMLNodeList = xmlDoc.SelectNodes(elementPattern)
                'If mLogger.IsDebugEnabled Then mLogger.Debug("XML Pattern Finder: " & vPattern & " Count: " & oXMLNodeList.Count)

                If (oXMLNodeList.Count > 0) Then
                    For Each oXMLNode In oXMLNodeList
                        If (oXMLNode.NodeType = XmlNodeType.Element) Then

                            strAttrValue = ""
                            If Not (oXMLNode.Attributes.GetNamedItem(vAttrPattern) Is Nothing) Then
                                strAttrValue = oXMLNode.Attributes.GetNamedItem(vAttrPattern).Value
                            End If

                            If (Len(strAttrValue) > 0) Then
                                ' colValue.Add(strAttrValue, CStr(iCounter))
                                colValue.Add(strAttrValue)
                                'iCounter = iCounter + 1
                            End If
                        End If
                    Next
                    GetAttributeValue = colValue
                End If
            Catch e As Exception
                If mLogger.IsFatalEnabled Then mLogger.Fatal(e.ToString)
            End Try
            GetAttributeValue = colValue
        End Function
0
 
LVL 1

Author Comment

by:DaveRowland
ID: 10975984
Thanks for the code.  I am working my way through it and understanding it.

Basically Danths, I am converting my old package from VB6 to VB.NET and trying to get the code fairly good on the way, it is a visual display system with moving messages, videos and so forth.  It get's it's "timeline" insutrctions from a INI which I am converting to XML.  These XML files control when certain things appear on the screen, previously I used to load the whole INI-sequence into the display app and just run through it.  However there is a little bit more to that, like when updates happen, detecting changes etc.

I have spent some of last year learning new concepts of XML and VB.NET but with so many changes, it is hard to keep on top of all the depth that is now in the programming environments.

The XML system was a good approach as later on I can use the Editor and create ASP.NET version of the editor and so forth that ties in and writes a XML which then can work over IP or even store it to FTP and deploy.

I have tested a little bit of the code above and can see how to use it.  thanks
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now