Solved

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

Posted on 2004-05-02
5
928 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

803 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