We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Problem with XPath Query in VB.NET

wmhogg007
wmhogg007 asked
on
Medium Priority
527 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>
Comment
Watch Question

Information Architect
CERTIFIED EXPERT
Top Expert 2006
Commented:
> 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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
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

Author

Commented:
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

Author

Commented:
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

Author

Commented:
(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
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.