2016-02-13 23 views
0

我遇到了如何通過搜索xml文檔來獲取特定值的問題。這是我第一次使用XML,我已經試過我搜索通過對計算器有以下兩個是什麼,我認爲我是最接近的解決方案與技術的所有方式:雖然在VB.NET中搜索XML

「#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文件:從「clientsslport」內/設置/收集者「的PostgreSQL

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

什麼,我希望做的是得到「8443」的價值觀「from」dbms「inside/repositoryconfig。我從整個文檔中加載了更多的值,但是一旦我瞭解瞭如何獲得它,我應該能夠重現它。

在此先感謝您的幫助或見解!

+0

如果你將數據放入對象可以付費使用反序列化,而不是兩種方法har07。有很多方法可以解析xml文檔,使用的方法將取決於您需要從xml中提取的數據量以及xml文件的大小。非常大的文件,你應該使用XMLReader這是另一種類似於har07響應的方法。 – jdweng

回答

0

「我正在尋找做的就是像值 」「 從 」clientsslport 8443「/settings/collector ......」

擔任首發,你可以提供從根元素到目標元素的完整路徑,同時使用@ +屬性名稱(而不是名稱)引用XML屬性(如果需要):

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 
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,將XDocument從LINQ到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到XML的工作, VB甚至提供特殊XML Axis Properties其中C#沒有:

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

啊 - 謝謝har07 !!!當然,代碼的作品,但關於使用xml和屬性的解釋是我之後的:)一旦我可以採取上述措施,並得到的東西工作,提取我需要的所有數據,並填充我的數據對象,我會更新! – Phaelo

+0

@Phaelo這些指向文檔的鏈接應該讓你開始瞭解.NET XML API中可用的類和方法,我將無法更好地解釋。然後,上述工作代碼應該讓您瞭解如何使用這些類和方法作爲「XmlDocument」的特定XML – har07

+0

,您可以獨立閱讀XPath語法,例如:http://www.w3schools.com /xsl/xpath_syntax.asp – har07