2017-02-18 77 views
-1

夥計們,我可以使用vb.net插入數據庫循環xml信息?如何Vb.net的XML循環

我嘗試一些少數的編解碼器,但循環不工作

<?xml version="1.0" encoding="UTF-8"?> 
<ComputerInformations> 
    <ComputerInformation> 
    <CompName>TESTUSER</CompName> 
    <CompModel>OptiPlex 990</CompModel> 
    <CompDomain>dnsx.local</CompDomain> 
    <CompSystemType>x64-based PC</CompSystemType> 
</ComputerInformation> 
<ComputerMemoryInformations> 
    <ComputerMemoryInformation> 
     <CompMemBank>BANK 1</CompMemBank> 
     <CompMemCapacity>2GB</CompMemCapacity> 
     <CompMemManufacturer>Hynix/Hyundai</CompMemManufacturer> 
     <CompMemPartNumber>HMT125U7BFR8C-G7</CompMemPartNumber> 
     <CompMemSerialNumber>1B73E64A</CompMemSerialNumber> 
     <CompMemSpeed>1067</CompMemSpeed> 
     <CompMemBank>BANK 2</CompMemBank> 
     <CompMemCapacity>2GB</CompMemCapacity> 
     <CompMemManufacturer>Hynix/Hyundai</CompMemManufacturer> 
     <CompMemPartNumber>HMT125U7BFR8C-G7</CompMemPartNumber> 
     <CompMemSerialNumber>1BB3E65E</CompMemSerialNumber> 
     <CompMemSpeed>1067</CompMemSpeed> 
    </ComputerMemoryInformation> 
    </ComputerMemoryInformations> 
</ComputerInformations> 

這是我的vb.net編解碼器,我使用vb.net 2015年:

Function ParseXmltoDatabase(ByVal XmlData As String) 
     Dim sr As New System.IO.StringReader(XmlData) 
     Dim reader = System.Xml.XmlReader.Create(sr) 
     reader.MoveToContent() 
     Dim inputXml = XDocument.ReadFrom(reader) 
     Dim XmlDoc = XDocument.Parse(inputXml.ToString) 
     Dim Xdoc As XmlDocument = New XmlDocument() 
     Xdoc.LoadXml(XmlDoc.ToString) 
     For Each node As XmlNode In Xdoc.SelectNodes("/ComputerInformations/ComputerMemoryInformations/ComputerMemoryInformation") 
     Dim ComputerMemoryInformation As XmlElement = TryCast(node, XmlElement) 
     Dim CompMemBank As XmlElement = DirectCast(ComputerMemoryInformation.SelectSingleNode("CompMemBank"), XmlElement) 
     If CompMemBank IsNot Nothing Then 
      Console.WriteLine("CompMemBank: {0}", CompMemBank.InnerText) 
     End If 
     Console.WriteLine() 
    Next 

問題是隻有我得到第一個結果CompMemBank the Each is not working

+0

您的XML格式不正確,我們無法猜測它應該是什麼。修復並再次詢問。 – MrGadget

+0

謝謝正確的XML添加到問題 – Chacaman

回答

1

太多的代碼......你需要的只是這個遍歷這些節點:

Sub ParseXmltoDatabase(ByVal XmlData As String) 
    With New XmlDocument() 
     .LoadXml(XmlData) 
     For Each node As XmlNode In .SelectNodes("/ComputerInformations/ComputerMemoryInformations/ComputerMemoryInformation/*") 
      Debug.Print(node.Name & vbTab & node.InnerText) 
     Next 
    End With 
End Sub 

問題的一部分是XPath在最後缺少/*