• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 209
  • Last Modified:

Searching for nodes using id attributes using xpathnavigator

Hi all,

I'm developing an automated localisation system in VB .NET, and I'm using an XML file for some of my parameters.

The following is a simplified version of the XML file I'm using:

<?xml version="1.0" encoding="UTF-8" ?>
   <versionInfo>
      <buildingBlocks>
         <buildingBlock id="1">
            <name>testname0</name>
            <buildingBlockGroup>group0</buildingBlockGroup>
            <files>
               <file id="00001" />
               <file id="00002" />
               <file id="00003" />
               ...
            </files>
         </buildingBlock>
         <buildingBlock id="222">
            <name>testname1</name>
            <buildingBlockGroup>group0</buildingBlockGroup>
            <files>
               <file id="00009" />
               <file id="00010" />
               <file id="00003" />
               <file id="00004" />
               ...
            </files>
         </buildingBlock>
         ...
      </buildingBlocks>
      <fileInfo>
         <files>
            <file id="00001" containsTranslatables="true" arch="mfc">
               <subpath>bin\fpl_str.exe</subpath>
               <language id="de-de" langType="source" lang="de-de" />
            </file>
            <file id="00002" containsTranslatables="true" arch="mfc">
               <subpath>bin\fpl_vdv.exe</subpath>
               <language id="de-de" langType="source" lang="de-de" />
            </file>
            <file id="00003" containsTranslatables="true" arch="java">
               <subpath>javalibs\de.ivu.mb.fpl.erh.jar</subpath>
               <language id="neutral" langType="source" lang="de-de" />
            </file>
            <file id="00008" containsTranslatables="true" arch="ISA">
               <subpath>dlg\fpl_afp.dlx</subpath>
               <language id="de-de" langType="source" lang="de-de" />
            </file>
            <file id="00009" containsTranslatables="false" arch="MFC">
               <subpath>dlg\fpl_afp.dll</subpath>
               <language id="de-de" langType="source" lang="de-de" />
            </file>
            ...
         </files>
     </fileInfo>
  </versionInfo>

For example, one of the things I want to do is retrieve some information from an individual /versionInfo/files/file node, on condition that its @arch value is either "java" or "mfc", and that its @containsTranslatables value is "true".

I need the following information:
1.  @arch attribute
2.  subpath element value
3.  language/@id and
4.  language/@lang

A search of file id="00003", for example, should yield a string array like this:
StringArray(0) -> "java"
StringArray(1) -> "javalibs\de.ivu.mb.fpl.erh.jar"
StringArray(2) ->"neutral"
StringArray(3) -> "de-de"

A search of file id="00009" should yield nothing.

Do I need four different XPath expressions*, each called separately from an XPath navigator, or can I do it more elegantly?

What is the best VB .NET class to use to implement the solution?  Should I use XPathNavigator?  And if so, what methods should I use?

Thanks very much in advance,

JaimeHy

*  Something like this?:  
"/versionInfo/files/file[@id=""" & fileID & """ and @containstranslatables=""true""
    and (@arch=""java"" or @arch="mfc")]/@arch", etc.
0
jaimehy
Asked:
jaimehy
  • 3
  • 3
1 Solution
 
jaimehyAuthor Commented:
Oops!

In the footnote above, marked with an asterix, I meant:

"/versionInfo/fileinfo/files/file[@id=""" & fileID & """ and @containstranslatables=""true""
    and (@arch=""java"" or @arch="mfc")]/@arch"
0
 
jaimehyAuthor Commented:
Or even:

"/versionInfo/fileinfo/files/file[@id=""" & fileID & """ and @containstranslatables=""true""
    and (@arch=""java"" or @arch=""mfc"")]/@arch"
0
 
Fernando SotoCommented:
Hi jaimehy;

This sample code should do what you need.

Imports System.Xml

        Dim doc As XmlDocument = New XmlDocument()
        doc.Load("C:\Temp\VersionInfo.xml")
        Dim root As XmlNode = doc.DocumentElement
        Dim StringArray(3) As String
        ' Get the info for a file with an ID entered into a text box and whos attributes
        ' containsTranslatables is true and arch os either java or mfc
        Dim node As XmlNode = root.SelectSingleNode("//fileInfo/files/file[@id=""" & tbID.Text & """ and (@containsTranslatables=""true"" and (@arch=""mfc"" or @arch=""java""))]")
        If Not node Is Nothing Then
            StringArray(0) = node.Attributes("arch").Value()
            StringArray(1) = node.SelectSingleNode("child::subpath").InnerText()
            StringArray(2) = node.SelectSingleNode("child::language").Attributes("id").InnerText()
            StringArray(3) = node.SelectSingleNode("child::language").Attributes("lang").InnerText()
        Else
            MessageBox.Show("File not found.")
        End If

Fernando
0
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!

 
Fernando SotoCommented:
The tbID.Text is a text box used to identify the file in question. It can be any text field that identifies the file you wish to search for.
0
 
jaimehyAuthor Commented:
Much obliged FernandoSoto,

I've put together a far less elegant solution since then, but I'll update it with your suggestion.

Thanks both for the speed and the thoroughness.

JaimeHy
0
 
Fernando SotoCommented:
Not a problem, glad I was able to help. ;=)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now