在VB.NET中搜索XML

时间:2016-02-13 05:04:09

标签: xml vb.net

我遇到了如何搜索xml文档以获取特定值的问题。这是我第一次使用XML,我尝试了各种技术,我在stackoverflow上搜索了以下两种技术,我认为我最接近解决方案:

“#1

Dim doc As New XmlDocument()
doc.Load(profilePath)
Dim nodelist As XmlNodeList = doc.SelectNodes("serverconfig/collectorconfig")

For Each node As XmlElement In nodelist
    MessageBox.Show(node("clientsslport").InnerText)
Next

“#2:

Dim aProfile As XmlDocument = New XmlDocument
Dim aProfileNodes As XmlNodeList
Dim aProfileNode As XmlNode

aProfile.Load(profilePath)
aProfileNodes = aProfile.SelectNodes("/clientsslport")
For Each aProfileNode In aProfileNodes
    frmDebug.lstDebug.Items.Add(aProfileNode.Attributes.GetNamedItem("clientsslport").Value.ToString)
    GLOBALDTSERVERS(profileNum).serverFatClientPort = aProfileNode.Attributes.GetNamedItem("clientsslport").Value.ToString
Next

目标是将函数指向XML文档,从xml获取值,然后将这些值加载到我创建的对象中。这是一个我为了隐私和长度而编辑的XML文件示例:

<dynatrace version="6.2.4.1057">
  <serverconfig memento.version="6.2.4.1057">
    <collectorconfig usetunnel="false" useproxy="false" authstring="" serverport="6698" groupname="" communicationport="8041" proxyport="8080" embedded="true" compress="true" communicationssl="true" useproxyauthentication="false" proxyhost="" proxyusername="" usepreemtiveproxyauth="true" name="Embedded dynaTrace Collector" watchdogtimeout="10" selfmoncollector="false" serveraddress="localhost" tunnel="http://localhost:8033/tunnel">
      <agentlistenaddressconfig>
        <listenaddress agentport="9998" agentaddress="" />
      </agentlistenaddressconfig>
      <loggingconfig append="true" correlationmaxbytes="31457280" console="INFO" level="INFO" maxfiles="5" correlationmaxfiles="2" path="../log/collector/dynaTrace Collector" html="false" maxbytes="10485760" />
      <buffers agentbuffersize="32768" />
      <protocoldumps maxnumberdumps="100" />
    </collectorconfig>
    <directories>
      <storedsessions path="sessions/stored" />
      <temp path="temp" />
      <reports path="reports" />
    </directories>
    <settings>
      <collector requirecompression="false" collectoraddress="" collectorport="5001" collectorssladdress="" collectorsslport="5443" allowcollectorconnections="true" collectorauthstring="" requiressl="false" />
      <client clientssladdress="" clientport="2020" clientsslport="8443" clientaddress="" requiressl="true" />
      <frontend frontendsslport="2031" frontendaddress="localhost" frontendrequiressl="true" frontendport="2030" />
      <server tunnelenabled="true" optimizeforthroughput="true" lastvalidedition="DYNATRACE_PRODUCTION" upperdeletiontriggerlimit="153600" collectortunnelenabled="true" continuoussessionrecording="true" lowerdeletiontriggerlimit="5120" collectortunneladdress="" fqdn="<IPADDRESS>" tunneladdress="" id="-1025570809" tunnelport="8023" jmxexportmanagement="false" jmxexportmonitoring="false" autodashboardsupport="true" jmxport="1099" runtimerevision="2" name="<IPADDRESS>" watchdogtimeout="10" collectortunnelport="6608" selfmonitoringenabled="true" agentbasedselfmonitoringenabled="true" hascollector="false" />
      <purepath maxnodes="10000" />
      <buffers pathwriter="524288" pathreader="1048576" measurementwriter="32768">
        <recentlystoredpaths maxsize="250000" maxage="300000" />
      </buffers>
      <oopanalyzer port="7788" address="" logpath="../log/analysisserver" />
      <profilebackups maxbackupfiles="10" />
    </settings>
    <loggingconfig append="true" correlationmaxbytes="31457280" console="INFO" level="INFO" maxfiles="5" correlationmaxfiles="2" path="../log/server" html="false" maxbytes="10485760" />
    <http externalhttpsport="-1" httpport="8080" externalhostname="" webservicesenabled="true" httpsport="8021" webserveraddress="" externalhttpport="-1" webserverenabled="true" webserveraddressssl="" requiressl="false" />
    <webstart tunnelenabled="false" proxyauthusername="" proxyautodetectionenabled="false" proxyport="-1" tunnelssl="false" proxyenabled="false" tunneladdress="<IPADDRESS>" tunnelport="8023" anonymouswebstartenabled="true" validationtimestamp="0" webstartenabled="true" proxyaddress="" proxyauthenabled="false" />
      <loggingconfig append="true" correlationmaxbytes="31457280" console="WARNING" level="INFO" maxfiles="5" correlationmaxfiles="2" path="../log" html="false" maxbytes="10485760" />
      <plugintypeconfig loglevel="INFO" sourcebundlename="com.dynatrace.diagnostics.plugin.SnmpMonitor" bundleversion="6.2.0.1201" name="SNMP Monitor Plugin" active="true" key=
    <webui webuihttpsport="9911" webuienabled="true" />
  </serverconfig>
  <environmentdescriptor memento.version="6.2.4.1057">
    <os arch="amd64" name="Linux" version="2.6.32-504.8.1.el6.x86_64" />
    <vm vendor="Oracle Corporation" name="Java HotSpot(TM) 64-Bit Server VM" version="24.80-b11" />
  </environmentdescriptor>
  <repositoryconfig memento.version="6.2.4.1057" lowduration="-1" highduration="1209600000" deletefromhigh="true" midduration="5184000000" querytimeout="7200">
    <repository>
      <database name="dynatrace62" dbms="PostgreSQL" />
      <credential user="<USER>" />
      <connection port="5432" usessl="false" host="<IP>" ignorewarnnonproduction="true" useurl="false" connectonstartup="true" embededdatapath="repository" url="<IP>" autopurge_measures="true" />
    </repository>
  </repositoryconfig>
</dynatrace>

我要做的是从/ settings / collector中的“clientsslport”获取“8443”等值,或者从/ repositoryconfig中的“dbms”获取“PostgreSQL”。我从完整的文档中加载了更多的值,但是一旦我知道如何获取它,我应该能够重现它。

提前感谢您提供任何帮助或见解!

1 个答案:

答案 0 :(得分:0)

  

&#34;我要做的是从&#34; clientsslport&#34;获取&#34; 8443&#34; 等值。 /settings/collector内...&#34;

作为入门者,您可以提供从根元素到目标元素的完整路径,同时使用@ +属性名称而不仅仅是名称来引用XML属性(如果需要): / p>

Dim xpath As String = "/dynatrace/serverconfig/settings/client/@clientsslport"
Dim nodelist As XmlNodeList = doc.SelectNodes(xpath)
For Each node As XmlNode In nodelist
    MessageBox.Show(node.Value)
Next

稍后,您可以使用descendant-or-self::node()缩写语法//缩短上述XPath表达式:

//client/@clientsslport

//大致意味着在当前XML文档中的任何位置查找以下节点/属性。

在使用XmlDocument走得太远之前,您可能需要考虑.NET较新的XML API,即LINQ-to-XML中的XDocument

Dim doc As XDocument = XDocument.Load(profilePath)
Dim ports As IEnumerable(Of XAttribute) = doc.Descendants("client").Attributes("clientsslport")
For Each port As XAttribute In ports 
    MessageBox.Show(port.Value)
Next

使用LINQ-to-XML,VB甚至提供了C#没有的特殊XML Axis Properties

Dim ports As IEnumerable(Of XAttribute) = doc...<client>.Attributes("clientsslport")