如何使用vb.net在Excel加载项中的embedded式XML文件上运行Xpath?

安装环境:

我正在使用.NET Framework 4使用vb.net开发Excel 2010应用程序级别加载项。

我的目标:

  1. 在运行期间,可以访问我的项目中的embedded式XML文件
  2. 使用XpathNavigator按名称select特定的节点

此代码在控制台应用程序中为我工作:

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() 

这里是我存储文件的地方:

目录

该文件位于bin / Debug目录中

当XML文件作为项目资源embedded时,如何做同样的事情?

文件路径

有人愿意就如何去做这件事提出build议吗? 我真的很感激。

为了解决我的问题,我从stream中读取XML数据:

  • 代码是:

     'xmldata.xml is stored in the bin/Debug directory docNav = New XPathDocument("xmldata.xml") 
  • 将其更改为:

     Dim stream As System.IO.StringReader stream = New StringReader(My.Resources.xmldata) docNav = New XPathDocument(stream)