问题描述
我在思考如何搜索 xml 文档以获取某些值时遇到问题.这是我第一次使用 XML,我尝试了我在 stackoverflow 上搜索过的各种技术,以下两个是我认为最接近解决方案的方法:
I am having issues with wrapping my head around how to search though a xml document to get certain values. It's my first time working with XML and I have tried all manner of techniques I've searched through on stackoverflow with the following two being what I think i am closest to a solution with:
'#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 文件,我已针对隐私和长度对其进行了
The goal is to point the function at a XML document, get values from the xml, and then load those values into object's i've created. Here is an example XML file that i've edited down for privacy and length:
<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"之类的值.我正在从完整文档中加载更多值,但是一旦我知道如何获取一个值,我应该能够重现它.
What i'm looking to do is get values like "8443" from "clientsslport" inside /settings/collector or "PostgreSQL" from "dbms" inside /repositoryconfig. I'm loading a lot more values from the full document but once I get an idea of how to get one, I should be able to reproduce it.
预先感谢您的任何帮助或见解!
Thanks in advance for any help or insights!
推荐答案
作为初学者,您可以只提供从根元素一直到目标元素的完整路径,同时使用 @
+ 属性名称,而不仅仅是名称,来引用 XML 属性,如果需要:
As a starter, you can just provide full path from the root element all the way to target element, while using @
+ attribute name, instead of just the name, to reference XML attribute, if needed :
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()
缩写语法//
:
Later, you can shorten the above XPath expression by using descendant-or-self::node()
abbreviated syntax //
:
//client/@clientsslport
//
大致意味着在当前 XML 文档中的任何位置查找以下节点/属性.
在使用 XmlDocument
之前,您可能需要考虑 .NET
的较新 XML API,XDocument
来自 LINQ-to-XML:
Before going too far with XmlDocument
, you might want to consider .NET
's newer XML API, the XDocument
from LINQ-to-XML:
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 甚至提供特殊的 XML 轴属性 哪个 C# 没有:
Working with LINQ-to-XML, VB even provides special XML Axis Properties which C# doesn't have :
Dim ports As IEnumerable(Of XAttribute) = doc...<client>.Attributes("clientsslport")
这篇关于在 VB.NET 中搜索 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!