Question

Setup Environment:

I'm developing an Excel 2010 Application Level Add-in using vb.net with .NET Framework 4.


My goal:

  1. During run-time, gain access to an embedded XML File in my project
  2. Use XpathNavigator to select specific nodes by name


This code worked for me in a Console Application:

Imports System.Xml
Imports System.Xml.XPath

        Dim nav As XPathNavigator
        Dim docNav As XPathDocument
        Dim NodeIter As XPathNodeIterator
        Dim strExpression As String

        'xmldata.xml is stored in the bin/Debug directory
        docNav = New XPathDocument("xmldata.xml")

        'Create a navigator to query with XPath.
        nav = docNav.CreateNavigator

        'For matching by Well Name
        strExpression = "/wellList/well/name[../name='GN-ALICE- 158-97-1324H-1']"

        'Select the result
        NodeIter = nav.Select(strExpression)


        Console.WriteLine("Well Name: {0}", NodeIter.Current.Value)

        'Show Results
        Console.ReadLine()


Here's where I stored the file:

Directory

The file is in the bin/Debug directory


How can I do the same thing when the XML file is embedded as a Project Resource?

FilePath


Would someone be willing to give a suggestion on how to go about doing this? I'd really appreciate it.

Was it helpful?

Solution

To solve my problem, I read XML Data from a stream:

  • Where the code is:

    'xmldata.xml is stored in the bin/Debug directory
    docNav = New XPathDocument("xmldata.xml")
    
  • Change it to this:

    Dim stream As System.IO.StringReader
    stream = New StringReader(My.Resources.xmldata)
    
    docNav = New XPathDocument(stream)
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top