?
Solved

Problem with XPath Query in VB.NET

Posted on 2006-03-29
5
Medium Priority
?
454 Views
Last Modified: 2008-02-01
Hi,

I have modified some of the Microsoft Windows Services Update Server VB.NET code to include an XPath query.  I am trying to return the UpdateID after passing the title like this:

Function GetUpdateID(ByVal CurrentTitle As String) As String

        Dim xpd As XPathDocument
        Dim xpn As XPathNavigator
        Dim xpni As XPathNodeIterator

        ' Load the Books.xml file
        xpd = New XPathDocument("UpdateDetails.xml")
        ' Get the associated navigator
        xpn = xpd.CreateNavigator()
        ' Retrieve nodes to match the expression
        xpni = xpn.Select("/Updates/Update[@Title = '" & CurrentTitle & "']/@UpdateIDCurrentUpdateID")
        'Return the UpdateID Value
        GetUpdateID = xpni.Current.Name

    End Function

The xpni.Current.Name is always empty.  I have tried the query with an XPath evaluator, and it seems to work fine.  Perhaps my formatting is wrong, or I am not using xpn.Select correctly.

Any suggestions to correct the above code, including a code snippet will be appreciated.

Thanks,

William

==========
Here is the entire code that I am using:

Imports Microsoft.UpdateServices.Administration
Imports System.Xml
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Xml.XPath

Module ComputerStatusToXML

    Sub Main()

        Dim Server As IUpdateServer
        Dim AdminProxy As AdminProxy
        Dim xml As Xml.XmlTextWriter
        Dim Computers As ComputerTargetCollection
        Dim InstallStatus As UpdateInstallationInfoCollection

        Try

            Console.WriteLine("Getting computer status...")

            'open the XML file
            xml = New XmlTextWriter(Environment.CurrentDirectory & "\ComputerStatus_NotApplicable " & DateTime.Now.ToLongDateString & ".xml", System.Text.Encoding.UTF8)
            xml.Formatting = Formatting.Indented
            xml.Indentation = 4

            xml.WriteStartDocument(True)
            '<ComputerStatus>
            xml.WriteStartElement("ComputerStatus")

            'connect to the local server
            AdminProxy = New AdminProxy
            Server = AdminProxy.GetUpdateServer

            'get a collection of the computers on this server
            Computers = Server.GetComputerTargets()

            'loop through the computer collection
            For Each computer As IComputerTarget In Computers
                'start the element for the computer
                xml.WriteStartElement("Computer")
                xml.WriteAttributeString("Name", computer.FullDomainName)
                xml.WriteAttributeString("LastReportedStatus", computer.LastReportedStatusTime.ToString)

                'get the install state of all updates approved for this computer
                InstallStatus = computer.GetUpdateInstallationInfoPerUpdate()

                'start the element for the status of updates on this computer
                xml.WriteStartElement("UpdateStatus")

                'loop through the updates in the install info collection and output the
                'name of the update and it's install state on this computer
                For Each UpdateStatus As IUpdateInstallationInfo In InstallStatus
                    xml.WriteStartElement("Update")
                    xml.WriteAttributeString("Title", UpdateStatus.GetUpdate.Title)
                    'Get UpdateID using XPath call
                    xml.WriteAttributeString("UpdateID", GetUpdateID(UpdateStatus.GetUpdate.Title))
                    Console.WriteLine(UpdateStatus.GetUpdate.Title)

                    xml.WriteAttributeString("Status", UpdateStatus.UpdateInstallationState.ToString)
                    '</Update>
                    xml.WriteEndElement()
                   
                Next
                'close the element </UpdateStatus>
                xml.WriteEndElement()

                'close the element </Computer>
                xml.WriteEndElement()

            Next
            'close the <ComputerStatus> element and the document
            xml.WriteEndElement()
            xml.Close()

            Console.WriteLine("Done. Results are written to the ComputerStatus {0}.xml file in the current folder", DateTime.Now.ToLongDateString)

        Catch ex As Exception
            Console.WriteLine("An error occured:" & vbCrLf & "Error text: " & ex.Message & vbCrLf & "Stack trace: " & ex.StackTrace & vbCrLf)
            xml.Close()
            End
        End Try

    End Sub
    Function GetUpdateID(ByVal CurrentTitle As String) As String

        Dim xpd As XPathDocument
        Dim xpn As XPathNavigator
        Dim xpni As XPathNodeIterator

        ' Load the  WSUS xml file
        xpd = New XPathDocument("UpdateDetails.xml")
        ' Get the associated navigator
        xpn = xpd.CreateNavigator()
        ' Retrieve nodes to match the expression
        xpni = xpn.Select("/Updates/Update[@Title = '" & CurrentTitle & "']/@UpdateIDCurrentUpdateID")
        'Return the UpdateID Value
        GetUpdateID = xpni.Current.Name.ToString

    End Function
End Module

========================
========================

Here is the format of the UpdateDetails.xml file:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Updates>
    <Update UpdateID="D5982290-F83A-47A4-B724-0033E1632125" Title="Security Update, February 14, 2002 (Internet Explorer 5.5)" Classification="Security Updates" LegacyName="q318089_w2k_ie55_5223" Description="This is an updated version of Security Update, February 14, 2002. This update resolves the &quot;Incorrect VBScript Handling can Allow Web Pages to Read Local Files&quot; security vulnerability in Internet Explorer 5.5 and Windows 2000, and is discussed in Microsoft Security Bulletin MS02-009. Download now to prevent a malicious user from using an unauthorized Web site to read the contents of files on your local computer." SyncDate="9/6/2005 10:09 AM" ReleaseDate="12/4/2003 4:56 PM" MsrcSeverity="Unspecified">
        <SecurityBulletins>
            <SecurityBulletin Number="MS02-009" />
        </SecurityBulletins>
        <KBArticles>
            <KBArticle ArticleNumber="318089" />
        </KBArticles>
        <AdditionalInformationUrls>
            <AdditionalInformationUrl URL="http://www.download.windowsupdate.com/msdownload/update/v3/static/RTF/en/5223.htm" />
        </AdditionalInformationUrls>
    </Update>
    <Update UpdateID="2E6CC164-D17D-4ADA-B8F2-E76B374D3479" Title="Security Update, February 14, 2002 (Internet Explorer 6)" Classification="Security Updates" LegacyName="q318089_w2k_xp_ie6_5226" Description="This is an updated version of Security Update, February 14, 2002. This update resolves the &quot;Incorrect VBScript Handling can Allow Web Pages to Read Local Files&quot; security vulnerability in Internet Explorer 6 and Windows XP or Windows 2000, and is discussed in Microsoft Security Bulletin MS02-009. Download now to prevent a malicious user from using an unauthorized Web site to read the contents of files on your local computer." SyncDate="9/6/2005 10:09 AM" ReleaseDate="4/9/2004 3:48 PM" MsrcSeverity="Unspecified">
        <SecurityBulletins>
            <SecurityBulletin Number="MS02-009" />
        </SecurityBulletins>
        <KBArticles>
            <KBArticle ArticleNumber="318089" />
        </KBArticles>
        <AdditionalInformationUrls>
            <AdditionalInformationUrl URL="http://www.download.windowsupdate.com/msdownload/update/v3/static/RTF/en/5226.htm" />
        </AdditionalInformationUrls>
    </Update>
    <Update UpdateID="D54FCD5E-68C0-42C2-8DBF-9A8A05A91929" Title="Security Update, February 13, 2002 (MSXML 4.0)" Classification="Security Updates" LegacyName="q317244_xml40_5255" Description="This update resolves the &quot;XMLHTTP Control Can Allow Access to Local Files&quot; security vulnerability in Microsoft XML (MSXML) 4.0 and is discussed in Microsoft Security Bulletin MS02-008. Download now to help prevent a malicious user from reading the files on your computer when you visit a specially malformed Web site." SyncDate="9/6/2005 10:09 AM" ReleaseDate="6/18/2003 3:26 PM" MsrcSeverity="Unspecified">
        <SecurityBulletins>
            <SecurityBulletin Number="MS02-008" />
        </SecurityBulletins>
        <KBArticles>
            <KBArticle ArticleNumber="317244" />
        </KBArticles>
        <AdditionalInformationUrls>
            <AdditionalInformationUrl URL="http://www.download.windowsupdate.com/msdownload/update/v3/static/RTF/en/5255.htm" />
        </AdditionalInformationUrls>
    </Update>
</Updates>
0
Comment
Question by:wmhogg007
  • 4
5 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 total points
ID: 16319614
> xpni = xpn.Select("/Updates/Update[@Title = '" & CurrentTitle & "']/@UpdateIDCurrentUpdateID")

I think you don't have an attribute with the name @UpdateIDCurrentUpdateID
so I guess there is a typo there

it would work if you did
xpni = xpn.Select("/Updates/Update[@Title = '" & CurrentTitle & "']/@UpdateID")

cheers
0
 

Author Comment

by:wmhogg007
ID: 16322659
Gertone,

Thanks for picking up the typo. I'm not sure how that got in there. Unfortunately, I am still getting no value returned by the XPath query.  Can you spot anything else that I may be doing wrong?

When I set a watch for xpni.Current.Value, I get only "" as a value returned.

This is what the function looks like now:

Function GetUpdateID(ByVal CurrentTitle As String) As String

        Dim xpd As XPathDocument
        Dim xpn As XPathNavigator
        Dim xpni As XPathNodeIterator

        ' Load the Books.xml file
        xpd = New XPathDocument("UpdateDetails.xml")
        ' Get the associated navigator
        xpn = xpd.CreateNavigator()
        ' Retrieve nodes to match the expression
        xpni = xpn.Select("/Updates/Update[@Title = '" & CurrentTitle & "']/@UpdateID")
        'Return the UpdateID Value
        GetUpdateID = xpni.Current.Value

    End Function

Thanks,

William
0
 

Author Comment

by:wmhogg007
ID: 16329213
Here is some additional information.

If I pass the query from a textbox, it works.  However, assembling the query like this:

xpni = xpn.Select("/Updates/Update[@Title = '" & CurrentTitle & "']/@UpdateID")

it does not work.

Any suggestions will be appreciated.

Thanks,

William
0
 

Author Comment

by:wmhogg007
ID: 16355674
Okay.  Here is the solution:

It is not the formatting of the XPath string, but rather the lack of .MoveNext in the code.  This code works:

Function GetUpdateID(ByVal CurrentTitle As String) As String

        Dim xpd As XPathDocument
        Dim xpn As XPathNavigator
        Dim xpni As XPathNodeIterator

        ' Load the Books.xml file
        xpd = New XPathDocument("UpdateDetails.xml")
        ' Get the associated navigator
        xpn = xpd.CreateNavigator()
        ' Retrieve nodes to match the expression
        xpni = xpn.Select("/Updates/Update[@Title = '" & CurrentTitle & "']/@UpdateID")
       
'Return the UpdateID Value
        GetUpdateID = xpni.Current.Value

    End Function
0
 

Author Comment

by:wmhogg007
ID: 16355709
(Disregard the previous entry - I hit the Enter key by accident before finishing the comment)

Okay.  Here is the solution:

It is not the formatting of the XPath string, but rather the lack of .MoveNext in the code.  This code works:

Function GetUpdateID(ByVal CurrentTitle As String) As String

        Dim xpd As XPathDocument
        Dim xpn As XPathNavigator
        Dim xpni As XPathNodeIterator

        ' Load the Books.xml file
        xpd = New XPathDocument("UpdateDetails.xml")
        ' Get the associated navigator
        xpn = xpd.CreateNavigator()
        ' Retrieve nodes to match the expression
        xpni = xpn.Select("/Updates/Update[@Title = '" & CurrentTitle & "']/@UpdateID")
        xpni.MoveNext  ' =====>  This is what was missing.  Now the XPathQuery works fine.
'Return the UpdateID Value
        GetUpdateID = xpni.Current.Value

    End Function
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month14 days, 18 hours left to enroll

840 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